- broadcasted :从父级作用域广播至子级 scope - $on()捕获事件
$broadcast('myevent')
- emitted :从子级作用域往上发射到父级作用域(相当于冒泡) - $on()捕获事件
$emit('myevent')
- 使用resolve传值,传递的是一个对象 - 获取时需要将传递的对象的名注入后使用
resolve: { academicinfo: function () { if (type === 'add') { return null } else if (type === 'edit') { return academic } } }
- $rootscope相当于一个全局变量,在同一个angular.module("exampleapp")下可用 - $rootscope需要注入到控制器后再使用
- 在同一个module下,不同的控制器可以访问同一个service服务 - 通过service服务里值的存储传递可实现控制器传值
完整demo:
<!doctype html> <html ng-app="exampleapp"> <head> <title>controllers</title> <script src="https://cdn.static.runoob.com/libs/angular.js/1.4.6/angular.min.js"></script> <link href="https://cdn.static.runoob.com/libs/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet" /> <script> angular.module("exampleapp", []) .service("zipcodes", function($rootscope) { return { setzipcode: function(type, zip) { this[type] = zip; $rootscope.$broadcast("zipcodeupdated", { type: type, zipcode: zip }); } } }) .controller("simplectrl", function ($scope, zipcodes) { $scope.$on("zipcodeupdated", function (event, args) { $scope[args.type] = args.zipcode; }); $scope.setaddress = function (type, zip) { zipcodes.setzipcode(type, zip); console.log("type: " + type + " " + zip); } $scope.copyaddress = function () { $scope.zip = $scope.billingzip; } }); </script> </head> <body> <p class="well" ng-controller="simplectrl"> <h4>billing zip code</h4> <p class="form-group"> <input class="form-control" ng-model="zip"> </p> <button class="btn btn-primary" ng-click="setaddress('billingzip', zip)"> save billing </button> </p> <p class="well" ng-controller="simplectrl"> <h4>shipping zip code</h4> <p class="form-group"> <input class="form-control" ng-model="zip"> </p> <button class="btn btn-primary" ng-click="copyaddress()"> use billing </button> <button class="btn btn-primary" ng-click="setaddress('shippingzip', zip)"> save shipping </button> </p> </body> </html>
以上五中angularjs控制器传值方案在工作中应该是够用啦,恩,点个赞呗:)
如对本文有疑问, 点击进行留言回复!!
轻松解决 org.apache.taglibs.standard.tlv.JstlCoreTLV 困惑
vert实践五——Json?Protocol Buffer?FlatBuffers?
[基于tensorflow的人脸检测] 基于神经网络的人脸检测8——验证训练好的神经网络
selenium + ajax抓取英雄联盟全部英雄的详细信息及多线程保存全部皮肤图片到本地
网友评论