gpt4 book ai didi

android - Ionic Popover无法将参数传递给 Controller

转载 作者:行者123 更新时间:2023-12-01 16:26:02 30 4
gpt4 key购买 nike

这是代码

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>

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

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

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

我在这里犯任何错误吗?还是什么?

最佳答案

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

但是,这是一个有效的代码段:

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>

关于android - Ionic Popover无法将参数传递给 Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35331130/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com