2016-06-27 24 views
1

我在模板中有两个控制器。我使用服务在他们之间传递数据。但是,使用文本框更新模型值时,仅在使用第一个控制器的第一个div上刷新(双向)文本。如何刷新第二个控制器中的数据

如何获得的第二个div数据刷新

  1. 不使用watchng-change
  2. 对于使用看

参考

  1. Using ng-change instead of $watch in Angular

<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>

+0

参考文献:[AngularJS - 控制器之间共享数据(http://excellencenodejsblog.com/angularjs-sharing-data-between-controller/) – Lijo

回答

1

你可以做的是值设置为服务于第一控制器和第二个使用该服务得到它。因此,它总是会接受存储在服务中的值。

<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" ng-change="myService.setName(valueA)"> 
 
\t <p>From First: {{myService.getName()}}</p> 
 
     </div> 
 
    </div> 
 
    <div class="container" style="padding-top: 20px;"> 
 
     <div ng-controller="mySECONDController"> 
 
      <p>From Second: {{myService.getName()}}</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) { 
 
      myService.setName("Lijo"); 
 

 
      $scope.myService= myService; 
 
\t  $scope.valueA = myService.getName(); 
 
     }); 
 

 
\t //defining controller 
 
     app.controller('mySECONDController', function ($scope, myService) {  
 
\t  $scope.myService= myService; 
 
     }); 
 
\t 
 

 

 
    </script> 
 
</body> 
 
</html>

相关问题