Angular.js$范围和控制器
Angular.js $scope and controller again
我知道关于angular$scope有很多问题,几个小时后我仍然不知道出了什么问题。我将Angular.js与Ionic一起使用,但问题应该与角度有关。
在我的应用程序中,我需要一个"添加到愿望列表"功能。在productDetail视图中,当有人点击"添加到愿望列表"时,我会将此产品与服务和webSQL一起添加到数据库中。现在,当用户转到愿望列表视图时,我会得到所有保存的产品,但该视图不会更新。
我有一个带有Ionic侧菜单的文件menu.html:
<ion-side-menu side="left" expose-aside-when="large" width="72">
<ion-content>
<ion-list ng-controller="LeftMenuCtrl">
<ion-item nav-clear menu-close ng-href="#/app/wish" ng-click="updateWishList()" ng-class="{'current': isActive('/app/wish')}">
<span class="icon icon-menu-wish"></span>
</ion-item>
</ion-list>
</ion-content>
</ion-side-menu>
"LeftMenuCtrl"将鼠标悬停在活动菜单项上。在我的WishListView中,我想显示当前添加的产品:文件wishList.html
<ion-view hide-nav-bar="true" view-title="WISHLIST" ng-controller="WishlistCtrl">
<div class="bar bar-header bar-stable">
<div class="header-wrapper">
<!--heading-->
<div class="dash-slider-wrapper"" >
<div class="header-text-main">WHISH</div> <div class="header-text-sub">LIST</div>
</div>
</div>
</div>
<ion-content class="has-header">
<ion-item class="product-card" ng-repeat="product in products">
<div class="card">
EAN: {{product.ean}}
</div>
</ion-item>
</ion-content>
</ion-view>
控制器:
app.controller("WishlistCtrl", function($scope, $state, productService) {
//gets called when the controller is loaded
productService.getWishlistProducts().then(function (products) {
$scope.products = products;
});
//gets called when the user clicks on the navi but does not
//update the $scope (or it updates the wrong $scope)
$scope.updateWishList = function () {
productService.getWishlistProducts().then(function (products) {
$scope.products = products;
});
};
})
有人能告诉我这里出了什么问题吗?
编辑:这是菜单链接的状态:
.state('app.wish', {
url: "/wish",
views: {
'menuContent': {
templateUrl: "templates/wish.html"
}
}
})
第2版:当我刷新页面时,我会看到所有添加的产品。但不是马上。它就像
$scope.updateWishList = function () ...
创建另一个$scope。。。
第3版:
app.controller('ProductDetailCtrl', function($stateParams ,$state, $scope, productService, $ionicHistory, $ionicPopup) {
$scope.addToWishlist = function(ean) {
productService.addProductToWishlist(ean).then(function(response) {
if(response == "OK") {
var alertPopup = $ionicPopup.alert({
title: 'product added to wishlist',
template: 'You can go to your wishlist to see all your marked products'
});
}
});
};
})
第4版:我在这里添加了一个Plunker:http://plnkr.co/edit/0woPfN它不起作用,但你可以在那里看到代码(我删除了不必要的东西)
您的问题是有两个WishlistCtrl
声明。
一旦进入app.js:
.state('app.wish', {
url: "/wish",
views: {
'menuContent': {
templateUrl: "wish.html",
controller: 'WishlistCtrl' //here is the first declaration
}
}
})
HTML中的第二个:
<ion-view hide-nav-bar="true" view-title="WISHLIST" ng-controller="WishlistCtrl">
去掉任何一个,你都会没事的。
最后,我让它工作起来了。首先在StateProvider中,我添加了一个解析方法:
.state('app.wish', {
url: "/wish",
views: {
'menuContent': {
templateUrl: "templates/wish.html",
controller: 'WishlistCtrl',
resolve: {
products: function(productService) {
return productService.getWishlistProducts()
}
}
}
}
})
当新产品被添加到愿望列表中时,更新型号:
.controller("WishlistCtrl", function($scope, $state, productService, products) {
//set wishlist products on controller load
$scope.products = products;
//updated wishlist when a new product gets added
$rootScope.$on('updatedWishList', function() {
console.log('List has been updated. ');
productService.getWishlistProducts().then(function (products) {
$scope.products = products;
});
});
})
简单。。。当你知道如何:D
相关文章:
- AngularJS控制器范围
- FileReader加载不在控制器范围Angular 2内
- Angular ES6控制器范围
- 离子单选按钮-控制器范围无法识别更改
- Angular:控制器范围内的可重用功能
- 角度嵌入和访问控制器范围
- 如何从 ng-repeat(ng-repeat)中的指令访问控制器范围
- 如何在Angular JS的这个小游戏中将对象同步到控制器范围
- AngularJS - 访问 Web 应用中的顶级控制器范围
- 应用程序的主控制器范围
- 设置另一个控制器范围变量而不使用 angular.element (Angularjs)
- 如何根据信号器请求的结果设置控制器范围
- 角度控制器范围项作为范围函数的子项
- 在 angularjs 控制器范围内声明函数和属性,但不附加到$scope
- AngularJS 控制器范围问题
- 理解angularJS中的控制器范围
- 如何将项目列表从创建许多选择标记的指令添加到控制器范围
- Angularjs:如何在指令中访问控制器范围
- 当使用Konacha进行测试时,Ember无法在didLoad回调中访问控制器范围
- 最好的做法是从出厂功能设置控制器范围变量