angularJS平行控制器间共享数据

<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8">
    <title></title>
</head>
<body ng-app="myApp">
    <div ng-controller="c1">
        {{name}} , {{msg}} , {{datas}}
    </div>
    <div ng-controller="c2">
        {{name}} , {{msg}} , {{datas}}
    </div>
</body>
<script src="angular.js"></script>
<script>
    var app = angular.module("myApp",[]);
    //factory和service的最大区别就是service所注入的类型必须为对象而factory可以是任意类型
    app.factory(custData1,function(){
        return{
            message:"共享数据!",
            Data:[a,b,c]
        }
    });
    app.controller("c1", function($scope,custData1){
        $scope.name="123";
        $scope.msg = custData1.message;
        $scope.datas = custData1.Data[1];
    });
    app.controller("c2", function($scope,custData1){
        $scope.name="456";
        $scope.msg = custData1.message;
        $scope.datas = custData1.Data[2];
    });
</script>
</html>

 

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。