Lyriene Lyriene - 4 months ago 301
AngularJS Question

Ionic Popover cannot pass parameter to controller

Here is the code

App.js

(...)

.state('app.dishdetails', {
url: '/menu/:id',
views: {
'mainContent': {
templateUrl: 'templates/dishdetail.html',
controller: 'DishDetailController'
}
}
});


Controller.js

.controller('DishDetailController', ['$scope', '$stateParams', 'menuFactory', 'favoriteFactory', 'baseURL','$ionicPopover', '$ionicListDelegate', '$ionicModal', function($scope, $stateParams, menuFactory, favoriteFactory, baseURL, $ionicPopover, $ionicListDelegate, $ionicModal) {

$scope.addFavoriteMenu = function(index){
console.log("index is " + index);
};

// .fromTemplateUrl() method
$ionicPopover.fromTemplateUrl('templates/dish-detail-popover.html', {
scope: $scope
}).then(function(popover) {
$scope.popover = popover;
});

$scope.openPopover = function($event) {
$scope.popover.show($event);
};

$scope.closePopover = function() {
$scope.popover.hide();
};

//Cleanup the popover when we're done with it!
$scope.$on('$destroy', function() {
$scope.popover.remove();
});

// Execute action on hide popover
$scope.$on('popover.hidden', function() {
// Execute action
});

// Execute action on remove popover
$scope.$on('popover.removed', function() {
// Execute action
});
}])


Main HTML

<ion-view view-title="Dish Details">
<ion-nav-buttons side="secondary">
<div class="buttons">
<button class="button button-icon icon ion-more"
ng-click="openPopover($event)"></button>
</div>
</ion-nav-buttons>
<ion-content>
(...)
</ion-content>
</ion-view>


Popover HTML

<ion-popover-view>
<ion-content>
<ul class="list">
<li class="item" ng-click="addFavoriteMenu({{dish.id}})">
Add to Favorites
</li>
</ul>
</ion-content>




db.json

"dishes": [
{"id": 0};
{"id": 1};
{"id": 2};
]


I can't get the parameter in the "addFavoriteMenu({{dish.id}})" in the controller.js, it always come out as undefined

but, when I try to inspect using Google Chrome, the ID could display properly in [ion-popover-view]

Is there any mistake that I create on here? or anything?

Answer

Trying to reproduce your issue I noticed that your JSON is wrong: there are semicolons (;) at the end of each line instead of colons (,).

Howevere here is a working snippet:

angular.module('ionicApp', ['ionic'])

.controller('AppCtrl', function($scope, $ionicPopover) {

  $scope.dishes = [
    {"id": 0},
    {"id": 1},
    {"id": 2}
  ];
  
  $scope.addFavoriteMenu = function(index) {
    console.log(index);
    alert("index is "+index);
  }
  
  $ionicPopover.fromTemplateUrl('templates/popover.html', {
    scope: $scope,
  }).then(function(popover) {
    $scope.popover = popover;
  });

});
<html ng-app="ionicApp">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="initial-scale=1, maximum-scale=1, user-scalable=no, width=device-width">
  <title>Ionic Popover</title>
  <link href="//code.ionicframework.com/nightly/css/ionic.css" rel="stylesheet">
  <script src="//code.ionicframework.com/nightly/js/ionic.bundle.js"></script>
</head>
<body class="platform-ios" ng-controller="AppCtrl">

  <div class="bar bar-header bar-positive">
    <h1 class="title">Popover</h1>
    <div class="buttons">
      <button class="button button-icon ion-more" ng-click="popover.show($event)">
      </button>
    </div>
  </div>
 
  <ion-content class="padding has-header">
    Click the more info icon in the top right. In an actual app the platform style will automatically be set.
  </ion-content>

  <script id="templates/popover.html" type="text/ng-template">
    <ion-popover-view>
      <ion-content>
        <ul class="list">
          <li ng-repeat="dish in dishes" class="item" ng-click="addFavoriteMenu({{dish.id}})">
            Add to Favorites {{dish.id}}
          </li>
        </ul>
      </ion-content>
    </ion-popover-view>
  </script>

</body>

</html>