[英]Sharing data between controllers in Angular, to watch or not watch?
I read the following questions and answers: Share data between AngularJS controllers , Update scope value when service data is changed , and Angular: Update service and share data between controllers . 我阅读了以下问题和解答: 在AngularJS控制器之间共享数据,在 更改服务数据时更新范围值 ,以及Angular:在控制器之间更新服务并共享数据 。
My question is about the answers provided. 我的问题是关于提供的答案。 The answers often suggest to
$watch
and I don't understand why. 答案通常暗示
$watch
,但我不明白为什么。
I wanted to share some values between controllers so I simply did this (this is a simplification of the code): 我想在控制器之间共享一些值,所以我简单地做到了这一点(这是代码的简化):
angular.module('app', [])
.factory('SomeSharedData', function()
{
return { 'pointer' : { 'someKey' : 'someValue' }};
})
.controller('Controller1', ['$scope', 'SomeSharedData', function($scope, SomeSharedData) {
$scope.pointer = SomeSharedData.pointer;
}])
.controller('Controller2', ['$scope', 'SomeSharedData', function($scope, SomeSharedData)
{
$scope.pointer = SomeSharedData.pointer;
}]);
Is this inheritly evil for some reason? 这是出于某种原因而继承的邪恶吗?
Why use something like: 为什么要使用类似:
$scope.$watch(function () { return SomeSharedData.someKey(); }, function (newValue) {
if (newValue) $scope.someKey = newValue;
});
It actually depends, in your case the pointer variable in the controller will be set to the SomeSharedData factory pointer variable. 实际上取决于您的情况,控制器中的指针变量将设置为SomeSharedData工厂指针变量。 So there is no need to use $watch in your case as the variable will be set once.
因此,在您的情况下无需使用$ watch,因为变量将被设置一次。
But, what if you want to do something if the pointer variable in the controller changes. 但是,如果控制器中的指针变量发生更改,您想做些什么,该怎么办? You will not know, whether the pointer variable has changed if you don't use $watch (that is what the $watch is for).
您将不知道,如果不使用$ watch(这就是$ watch的目的),则指针变量是否已更改。
Like, take an example. 像,举个例子。 Here i have created a model called pointer and i am watching for changes, and if anything changes the count is increased by 1 everytime.
在这里,我创建了一个称为指针的模型,我正在监视更改,如果发生任何更改,则每次计数都会增加1。 In this case the $watch is necessary.
在这种情况下,需要$ watch。 So, if in any case you want to do something if the model is changes, you can use $watch.
因此,如果在任何情况下您都想更改模型,则可以使用$ watch。
index.html 的index.html
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Angular $watch</title>
<script src="//ajax.googleapis.com/ajax/libs/angularjs/1.2.25/angular.min.js"></script>
</head>
<body>
<div ng-app="myApp">
<div ng-controller="myCtrl">
<input type="text" ng-model="pointer">
{{pointer}}
<br>
<h3>Will increase if input changes:<h1>{{count}}</h1></h3>
</div>
</div>
<script src="main.js"></script>
</body>
</html>
main.js main.js
var myApp = angular.module('myApp', []);
myApp.controller('myCtrl', ['$scope', function ($scope) {
$scope.count = 1;
$scope.$watch('pointer', function(newValue, oldValue) {
if ( newValue !== oldValue ) {
$scope.count += 1;
}
});
}]);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.