angular中不同controller传值问题

利用angularJS中service单例模式的特性,服务(service)提供了一种能在应用的整个生命周期内保持数据的方式,能够在控制器之间进行通信,且能保证数据的一致性。

一般我们都会封装server来为应用提供访问数据的接口,或者跟远程进行数据交互。

示例

复制代码
JavaScript

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";   } });
复制代码

 


 

参考文章:《angularJS的controller之间如何正确的通信》

posted @ 2016-09-18 13:29  TZZmeing  阅读(144)  评论(0编辑  收藏  举报