[英]AngularJS how to setup two way data binding between a service and a controller
How would you on connect two way data binding between a service and controller? 您将如何在服务和控制器之间连接双向数据绑定? I've looked at a couple posts, such as How to make two-way data binding between service and controller , but I don't understand the answer. 我看过几篇文章,例如“ 如何在服务和控制器之间进行双向数据绑定” ,但我不明白答案。 Can someone provide a simple, high-level explanation or example? 有人可以提供简单的高级解释或示例吗? I've looked into using $watch, but I've also noticed a lot of people saying it shouldn't be used in controllers ie. 我已经研究过使用$ watch了,但是我也注意到很多人说它不应该在控制器中使用。 Angular JS - you probably shouldn't use $watch in your controllers , which just adds to the confusion since I don't know where else I would add it. Angular JS-您可能不应该在控制器中使用$ watch ,这只会增加混乱,因为我不知道我还要在哪里添加它。
Included dependencies from main app.js 包含来自主app.js的依赖项
(function() {
'use strict';
angular.module('myApp', [
'ngRoute',
'app.controller',
'app.service',
'component.navbar'
]);
})();
Included dependencies from navbar component 导航栏组件包含的依赖项
(function() {
'use strict';
/* Navbar Component */
angular.module('component.navbar', [
'component.navbar.controller',
'component.navbar.directive',
'component.navbar.service'
])
.run(function( NavbarService ) {
NavbarService.getJSON();
});
})();
Navbar component snippets Navbar组件片段
(function() {
'use strict';
angular.module('component.navbar.service', [])
.factory('NavbarService', ['$http', function( $http ) {
var navbarJSON = [];
var active = 0;
var getJSON = function() {
return $http.get('app/data/navlinks.json')
.success( function( data ) {
navbarJSON = data;
});
}
var getData = function( ) {
return navbarJSON;
}
var setActive = function( index ) {
active = index;
}
var getActive = function() {
return active;
}
return {
getJSON: getJSON,
getData: getData,
setActive: setActive,
getActive: getActive
}
}]);
})();
(function() {
'use strict';
angular.module('component.navbar.controller', [])
.controller('NavbarController', ['$scope', 'NavbarService', function( $scope, NavbarService ) {
$scope.navbarData = NavbarService.getJSON();
$scope.active = NavbarService.getActive();
$scope.setActive = function( index ) {
//$scope.active = $scope.navbarData[index]
NavbarService.setActive( index );
}
}]);
})();
You could have just chosen to pass the service as a $scope
property and then use its methods as it is: 您可能只是选择将服务作为$scope
属性传递,然后按原样使用其方法:
Javascript 使用Javascript
Controller 调节器
.controller('NavbarController', ['$scope', 'NavbarService', function( $scope, NavbarService ) {
NavbarService.getJSON();
$scope.navbar = NavbarService;
}]);
HTML (Example) HTML (示例)
<nav ng-controller="NavbarController">
<a ng-repeat="link in navbar.getData()"
ng-class="{'active': navbar.getActive() == $index}"
ng-click="navbar.setActive($index)">
{{link.label}}
</a>
</nav>
You need to import your service module to your controller module first 您需要先将服务模块导入到控制器模块中
angular.module('component.navbar.controller', ['component.navbar.service']) angular.module('component.navbar.controller',['component.navbar.service'])
and I can't see where you have called NavBarService.getJSON () , so called it first to get the data. 而且我看不到您在哪里调用NavBarService.getJSON() ,因此首先调用它来获取数据。
Why not just expose active
in your service? 为什么不公开服务中的active
? If you need getter/setter, use implicit ones. 如果需要getter / setter,请使用隐式的。
Regarding the suggestion not to use $watch
in controllers. 关于不要在控制器中使用$watch
的建议。 That is a pretty generic statement. 那是一个非常普通的陈述。 If $watch
is the right tool, use it. 如果$watch
是正确的工具,请使用它。 If you can achieve the same with an event listener, do that. 如果可以使用事件侦听器实现相同的功能,请执行此操作。
In your case your service could broadcast an event on the root scope to signal a change of state. 在您的情况下,您的服务可以在根作用域上广播事件以发出状态更改信号。 You could listen for that event in your controller and update your value instead of using $watch
. 您可以在控制器中监听该事件并更新您的值,而不用使用$watch
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.