1
我在模板中有两个控制器。我使用服务在他们之间传递数据。但是,使用文本框更新模型值时,仅在使用第一个控制器的第一个div上刷新(双向)文本。如何刷新第二个控制器中的数据
如何获得的第二个div数据刷新
- 不使用
watch
或ng-change
- 对于使用看
参考:
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.2.27/angular.min.js">
</script>
</head>
<body ng-app="myApp">
<div class="container" style="padding-top: 20px;">
<div ng-controller="myController">
\t <input type="text" ng-model="valueA">
\t <p>From First: {{valueA}}</p>
</div>
</div>
<div class="container" style="padding-top: 20px;">
<div ng-controller="mySECONDController">
<p>From Second: {{valueB}}</p>
</div>
</div>
<script>
//defining module
var app = angular.module('myApp', []);
//defining service
app.service('myService', function() {
this.name = '';
this.setName = function (newName)
\t {
this.name = newName;
};
\t this.getName = function()
\t {
return this.name;
};
}
\t);
//defining controller
app.controller('myController', function ($scope, myService) {
\t myService.setName("Lijo");
\t $scope.valueA = myService.getName();
});
\t //defining controller
app.controller('mySECONDController', function ($scope, myService) {
\t $scope.valueB = myService.getName();
});
\t
</script>
</body>
</html>
参考文献:[AngularJS - 控制器之间共享数据(http://excellencenodejsblog.com/angularjs-sharing-data-between-controller/) – Lijo