AngularJS 多次使用 Controller 和 rootScope [英] AngularJS multiple uses of Controller and rootScope
问题描述
我想在 2 个单独的 HTML 元素上使用控制器,并在编辑时使用 $rootScope 使 2 个列表保持同步:
HTML
<div ng-controller="菜单"><input type="text" id="newItem" value=""/><input type="submit" ng-click="addItem()"/><ul class="nav" ng-controller="Menu"><li ng-repeat="菜单项"><a href="{{item.href}}">{{item.title}}</a>
JS
angular.module('menuApp', ['menuServices']).运行(函数($rootScope){$rootScope.menu = [];});angular.module('menuServices', ['ngResource']).工厂('MenuData',函数($资源){返回$资源('/工具/菜单.cfc',{返回格式:'json'},{获取菜单:{方法:'获取',参数:{方法:'getMenu'}},新增项目: {方法:'获取',参数:{方法:'addItem'}}});});功能菜单($scope,MenuData){//尝试添加新项目$scope.addNewItem = function(){var thisItem = $('#newItem').val();MenuData.addItem({item: thisItem},function(data){$scope.updateMenu();});}$scope.updateMenu = function() {MenuData.getMenu({},function(data){$scope.menu = data.MENU;});}//获取菜单数据$scope.updateMenu();}
当页面加载时,UL
和 DIV
都显示数据库中的正确内容,但是当我使用 addNewItem()
方法仅更新 DIV
.
是否有更好的方法来构建我的逻辑,或者我可以做些什么来确保 UL
中的 $scope.menu
同时更新?
这里有一个类似的例子:http://plnkr.co/edit/2a55gq
我建议使用包含菜单及其方法的服务.该服务将更新控制器引用的菜单.
在此处查看工作 plunker:http://plnkr.co/edit/Bzjruq
这是示例 JavaScript 代码:
角度.module('sampleApp', [] ).service( 'MenuService', [ '$rootScope', function( $rootScope ) {返回 {菜单:['项目1'],添加:功能(项目){this.menu.push( item );}};}]).controller('ControllerA', ['MenuService', '$scope', function(MenuService, $scope) {$scope.menu = MenuService.menu;$scope.addItem = function() {MenuService.add( $scope.newItem );};}]);
和示例 Html 页面:
<head lang="zh-cn"><meta charset="utf-8"><title>自定义Plunker</title><script src="//ajax.googleapis.com/ajax/libs/angularjs/1.0.2/angular.min.js"></script><script src="app.js"></script>头部><body ng-app="sampleApp"><div ng-controller="ControllerA"><ul><li ng-repeat="菜单中的项目">{{item}}</li><input type="text" ng-model="newItem"/><input type="submit" ng-click="addItem()"/>
<div ng-controller="ControllerA"><ul><li ng-repeat="菜单中的项目">{{item}}</li>
</html>
I want to use a controller on 2 seperated HTML elements, and use the $rootScope to keep the 2 lists in sync when one is edited:
HTML
<ul class="nav" ng-controller="Menu">
<li ng-repeat="item in menu">
<a href="{{item.href}}">{{item.title}}</a>
</li>
</ul>
<div ng-controller="Menu">
<input type="text" id="newItem" value="" />
<input type="submit" ng-click="addItem()" />
<ul class="nav" ng-controller="Menu">
<li ng-repeat="item in menu">
<a href="{{item.href}}">{{item.title}}</a>
</li>
</ul>
</div>
JS
angular.module('menuApp', ['menuServices']).
run(function($rootScope){
$rootScope.menu = [];
});
angular.module('menuServices', ['ngResource']).
factory('MenuData', function ($resource) {
return $resource(
'/tool/menu.cfc',
{
returnFormat: 'json'
},
{
getMenu: {
method: 'GET',
params: {method: 'getMenu'}
},
addItem: {
method: 'GET',
params: {method: 'addItem'}
}
}
);
});
function Menu($scope, MenuData) {
// attempt to add new item
$scope.addNewItem = function(){
var thisItem = $('#newItem').val();
MenuData.addItem({item: thisItem},function(data){
$scope.updateMenu();
});
}
$scope.updateMenu = function() {
MenuData.getMenu({},function(data){
$scope.menu = data.MENU;
});
}
// get menu data
$scope.updateMenu();
}
When the page loads, both the UL
and the DIV
display the correct contents from the database, but when i use the addNewItem()
method only the DIV
gets updated.
Is there a better way to structure my logic, or can I do something to make sure the $scope.menu
in the UL
gets updated at the same time?
Here's an example of something similar: http://plnkr.co/edit/2a55gq
I would suggest to use a service that holds the menu and its methods. The service will update the menu which is referenced by the controller(s).
See a working plunker here: http://plnkr.co/edit/Bzjruq
This is the sample JavaScript code:
angular
.module( 'sampleApp', [] )
.service( 'MenuService', [ '$rootScope', function( $rootScope ) {
return {
menu: [ 'item 1' ],
add: function( item ) {
this.menu.push( item );
}
};
}])
.controller( 'ControllerA', [ 'MenuService', '$scope', function( MenuService, $scope ) {
$scope.menu = MenuService.menu;
$scope.addItem = function() {
MenuService.add( $scope.newItem );
};
}]);
And the sample Html page:
<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="utf-8">
<title>Custom Plunker</title>
<script src="//ajax.googleapis.com/ajax/libs/angularjs/1.0.2/angular.min.js"></script>
<script src="app.js"></script>
</head>
<body ng-app="sampleApp">
<div ng-controller="ControllerA">
<ul>
<li ng-repeat="item in menu">{{item}}</li>
</ul>
<input type="text" ng-model="newItem" /><input type="submit" ng-click="addItem()" />
</div>
<div ng-controller="ControllerA">
<ul>
<li ng-repeat="item in menu">{{item}}</li>
</ul>
</div>
</body>
</html>
这篇关于AngularJS 多次使用 Controller 和 rootScope的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!