html

<input type="text" ng-model="person.name"/>
  <div ng-controller="FirstCtrl">
  {{person.name}}
  <button ng-click="setName()">set name to jack</button>
  </div>
  <div ng-controller="SecondCtrl">
  {{person.name}}
  <button ng-click="setName()">set name to jack</button>
  </div>
</div>

js

var myApp = angular.module("myApp", []);
myApp.factory('Data', function() {
  return {
    name: "Ting"
  }
});
myApp.controller('FirstCtrl', function($scope, Data) {
  $scope.data = Data;

  $scope.setName = function() {
    Data.name = "Jack";
  }
});

myApp.controller('SecondCtrl', function($scope, Data) {
  $scope.data = Data;

  $scope.setName = function() {
    Data.name = "Moby";
  }
});

发表评论

电子邮件地址不会被公开。 必填项已用*标注

此站点使用Akismet来减少垃圾评论。了解我们如何处理您的评论数据