Ionic Popover无法将参数传递给控制器

时间:2016-02-11 05:01:58

标签: android ios angularjs ionic-framework popover

这是代码

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
           });
            }])

主要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>

弹出广告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};
]

我无法在controller.js中的“addFavoriteMenu({{dish.id}})”中获取参数,它总是以未定义的形式出现

但是,当我尝试使用Google Chrome进行检查时,ID可以在[ion-popover-view]中正确显示

我在这里创造了什么错误?还是什么?

1 个答案:

答案 0 :(得分:0)

尝试重现您的问题我注意到您的JSON错误:每行末尾都有分号(;)而不是冒号(,)。

Howevere这是一个工作片段:

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>