网站的联系我们怎么做,杭州滨江网站制作,怎么免费做网站不要域名,深圳 做网站 互联刚刚看了网上的一些关于控制器之间的通信#xff1b;然后结合自己项目做了一些#xff0c;这里主要做的是二个同级之间的controller通信。 Html#xff1a; 1 html2 script srchttp://apps.bdimg.com/libs/angular.js/1.3.9/angular.min.js… 刚刚看了网上的一些关于控制器之间的通信然后结合自己项目做了一些这里主要做的是二个同级之间的controller通信。 Html 1 html2 script srchttp://apps.bdimg.com/libs/angular.js/1.3.9/angular.min.js/script3 body4 div ng-appapp5 div ng-controllerchildCtr1name :6 input ng-modelname typetext /7 /div8 div ng-controllerchildCtr2Ctr1 name:9 {{ctr1Name}}
10 /div
11 /div
12 /body
13 /html 上面的html代码设置了2个同级的控制器现在childCtr2需要childCtr1的参数来显示相关信息下面是控制器childCtr1的代码 1 angular.module(app, [])
2 .controller(childCtr1, function ($scope) {
3 $scope.$watch(name,function (){//监听绑定“name当发生改变时发送消息
4 //alert(123);
5 $scope.$emit(Ctr1NameChange, $scope.name);//发送名为Ctr1NameChange的消息值为$scope.name
6 });
7 }); 下面是childCtr2的代码 angular.module(app, [])
.controller(childCtr2, function ($scope) {$scope.$on(Ctr1NameChange,//监听有没有名为”Ctr1NameChange“的消息如果有则执行下面函数function (event, msg) {console.log(childCtr2, msg);$scope.ctr1Name msg;});
}); 以上是针对控制器分离的文件写的不同形式当然也可以将他们合并到一个页面 1 angular.module(app, [])2 .controller(childCtr1, function ($scope) {3 $scope.$watch(name,function (){4 //alert(123);5 $scope.$emit(Ctr1NameChange, $scope.name);6 });7 }).controller(childCtr2, function ($scope) {8 $scope.$on(Ctr1NameChange,9
10 function (event, msg) {
11 console.log(childCtr2, msg);
12 $scope.ctr1Name msg;
13 });
14 }); 还有很多控制器之间的通信方法包括server的factory可以参考http://jsbin.com/hopazo/5/edit?html,css,js,output 转载于:https://www.cnblogs.com/tiedaweishao/p/4798651.html