{"id":1103,"date":"2014-11-19T21:04:14","date_gmt":"2014-11-19T13:04:14","guid":{"rendered":"http:\/\/www.storagelab.org.cn\/zhangdi\/?p=1103"},"modified":"2014-11-19T21:04:14","modified_gmt":"2014-11-19T13:04:14","slug":"%e4%b8%8d%e5%90%8ccontroller%e4%bd%9c%e7%94%a8%e5%9f%9f%e4%b9%8b%e9%97%b4%e9%80%9a%e4%bf%a1%e7%9a%84%e6%96%b9%e5%bc%8f","status":"publish","type":"post","link":"https:\/\/www.divis.cn\/index.php\/2014\/11\/19\/%e4%b8%8d%e5%90%8ccontroller%e4%bd%9c%e7%94%a8%e5%9f%9f%e4%b9%8b%e9%97%b4%e9%80%9a%e4%bf%a1%e7%9a%84%e6%96%b9%e5%bc%8f\/","title":{"rendered":"ngularJs\u9879\u76ee\u5b9e\u6218\uff0105: \u4e0d\u540ccontroller\u4f5c\u7528\u57df\u4e4b\u95f4\u901a\u4fe1\u7684\u65b9\u5f0f"},"content":{"rendered":"<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone\" alt=\"\" src=\"http:\/\/static.oschina.net\/uploads\/img\/201407\/29063140_xqHg.png\" width=\"467\" height=\"262\" \/><\/p>\n<p>&nbsp;<\/p>\n<p>\u6700\u8fd1\u5728\u505ad3js + angularjs\u9879\u76ee\u4e2d\uff0c\u7ecf\u5e38\u9047\u5230d3\u7ec4\u4ef6\u4e0eangularjs\u6a21\u5757\u95f4\u901a\u4fe1\u7684\u95ee\u9898\uff0c\u4ee5\u53caangularjs\u591a\u4e2a\u4f5c\u7528\u57df\u4e4b\u95f4\u4e92\u76f8\u901a\u4fe1\u7684\u95ee\u9898\u3002\u5173\u4e8eangularjs\u7684\u4f5c\u7528\u57df\u6982\u5ff5\u53ca\u5176\u7ee7\u627f\u6a21\u5f0f\uff0c\u8fd9\u91cc\u6709\u4e00\u7bc7\u6211\u89c9\u5f97<a href=\"http:\/\/www.lovelucy.info\/understanding-scopes-in-angularjs.html\">\u4e0d\u9519\u7684\u6587\u7ae0<\/a>\uff0c\u4e0d\u4e86\u89e3\u7684\u670b\u53cb\u53ef\u4ee5\u5148\u53bb\u770b\u770b\u3002<\/p>\n<p>\u672c\u6587\u4e3b\u8981\u8c08angularjs\u591a\u4e2a\u4f5c\u7528\u57df\u4e4b\u95f4\u5982\u4f55\u4e92\u76f8\u901a\u4fe1\u3002\u6211\u4eec\u7ecf\u5e38\u9047\u5230\u8fd9\u6837\u7684\u9700\u6c42\uff1aA\u4f5c\u7528\u57df\u8fd9\u91cc\u6709\u4e00\u4e2a\u503c\u6539\u53d8\u4e86\uff0c\u5982\u4f55\u901a\u77e5\u4f5c\u7528\u57dfB\u76f8\u5e94\u503c\u53bb\u6539\u53d8\u3002\u4e3a\u6b64\u6211\u4e00\u76f4\u5728\u5bfb\u627e\u6700\u4f73\u5b9e\u8df5\uff0c\u5c24\u5176\u662f\u5bf9\u4e8e\u4f5c\u7528\u57df\u5f88\u591a\uff0c\u5305\u542b\u5173\u7cfb\u590d\u6742\u7684\u60c5\u51b5\u3002\u4ece\u7b80\u5355\u5230\u590d\u6742\uff0c\u65b9\u6cd5\u603b\u7ed3\u5982\u4e0b\uff1a<\/p>\n<h2>1.$rootscope<\/h2>\n<p>\u5927\u5bb6\u90fd\u77e5\u9053$scope\u662fhtml\u548c\u5355\u4e2acontroller\u4e4b\u95f4\u7684\u6865\u6881\uff0c\u6570\u636e\u7ed1\u5b9a\u5c31\u9760\u4ed6\u4e86\u3002\u800c$rootscope\u53ef\u4ee5\u88ab\u8ba4\u4e3a\u662f\u5168\u5c40$scope, \u5728\u5404\u4e2acontroller\u91cc\u9762\u90fd\u53ef\u4ee5\u663e\u793a\uff0c\u4e5f\u90fd\u53ef\u4ee5\u4fee\u6539\u3002<\/p>\n<p>\u4e0b\u4f8b\u5c55\u793a\u4e86\u5982\u4f55\u5728$rootscope\u4e0a\u521b\u5efa\u4e00\u4e2a\u5bf9\u8c61\u548c\u4f7f\u7528\u5176\u4e2d\u7684\u6570\u636e\uff1a<\/p>\n<pre class=\"lang:default decode:true\">angular.module('myApp', [])\n.run(function($rootScope) {\n    $rootScope.test = new Date();\n})\n.controller('myCtrl', function($scope, $rootScope) {\n  $scope.change = function() {\n        $scope.test = new Date();\n    };\n\n    $scope.getOrig = function() {\n        return $rootScope.test;\n    };\n})\n.controller('myCtrl2', function($scope, $rootScope) {\n    $scope.change = function() {\n        $scope.test = new Date();\n    };\n\n    $scope.changeRs = function() {\n        $rootScope.test = new Date();\n    };\n\n    $scope.getOrig = function() {\n        return $rootScope.test;\n    };\n});<\/pre>\n<p><strong>\u4f18\u70b9:<\/strong><\/p>\n<ul>\n<li>\u7b80\u5355\u6613\u61c2<\/li>\n<\/ul>\n<p><strong>\u7f3a\u70b9:<\/strong><\/p>\n<ul>\n<li>\u5168\u5c40\u53d8\u91cf\u6c61\u67d3<\/li>\n<\/ul>\n<p><strong>\u9002\u7528\u8303\u56f4:<\/strong><\/p>\n<p>\u9891\u7e41\u7684\u4f7f\u7528$rootscope\u4f1a\u9020\u6210\u5168\u5c40\u53d8\u91cf\u6c61\u67d3\uff0c\u4f46\u6211\u4e5f\u53cd\u5bf9\u90e8\u5206\u4ee3\u7801\u6d01\u7656\u8005\u5b8c\u5168\u62d2\u7edd$rooScope\u7684\u4f5c\u98ce\u3002\u4e00\u4e9b\u7279\u522b\u9891\u7e41\u8c03\u7528\u7684\u65b9\u6cd5\uff0c\u5b8c\u5168\u53ef\u4ee5\u8be5\u653e\u5728$rootscope\u91cc\u3002\u5bf9\u4e8e\u5c11\u91cf\u4e00\u65e6\u767b\u5f55\u5c31\u4f1a\u5230\u5904\u663e\u793a\uff0c\u5e76\u4e14\u4e0d\u592a\u5bb9\u6613\u53d8\u5316\u7684\u53d8\u91cf\uff0c\u5b8c\u5168\u53ef\u4ee5\u4f7f\u7528$rootScope\u6765\u4fdd\u5b58\u3002\u4f8b\u5982\u7cfb\u7edf\u7684\u767b\u5f55\u7528\u6237\u540d\uff0c\u4e00\u822c\u767b\u5f55\u4ee5\u540e\u5c31\u57fa\u672c\u4e0d\u4f1a\u53d8\uff0c\u8fd8\u8981\u5728\u5404\u4e2a\u4f5c\u7528\u57df\u4e2d\u663e\u793a\u3002\u90a3\u4e48\u5bf9\u4e8e\u5c11\u91cf\u6b64\u7c7b\u53d8\u91cf\uff0c\u4e3a\u4f55\u4e0d\u7528$rootScope\u6765\u50a8\u5b58\u5462\uff1f\u9664\u6b64\u4ee5\u5916\uff0c$rootScope\u8fd8\u6709\u4e00\u4e2a\u7279\u522b\u6709\u7528\u7684\u7279\u6027\uff0c\u90a3\u5c31\u662f\u5b83\u5904\u4e8e\u6240\u6709scope\u7684\u6700\u9876\u5c42\uff0c\u5728\u4e8b\u4ef6\u4f20\u64ad\u4e2d\u6709\u5999\u7528\uff0c\u5728\u4e00\u4e2a\u901a\u7528\u7684\u8ba2\u9605\/\u53d1\u5e03\u6a21\u5f0f\u7684angularjs\u901a\u4fe1\u6a21\u5757\u4e2d\uff0c\u51e0\u4e4e\u5c11\u4e0d\u4e86\u4f7f\u7528$rootScope\u3002\u8fd9\u4e00\u70b9\u5728\u540e\u6587\u4e2d\u4f1a\u6709\u8be6\u7ec6\u63cf\u8ff0\u3002<\/p>\n<h2>2.\u4f5c\u7528\u57df\u7ee7\u627f<\/h2>\n<p>\u4f5c\u7528\u57df\u5d4c\u5957\u5e26\u6765\u7684\u7236\u5b50\u4f5c\u7528\u57df\u7684\u7ee7\u627f\u5173\u7cfb\u4e5f\u53ef\u4ee5\u7b97\u662f\u4e00\u79cd\u7236\u5b50\u4f5c\u7528\u57df\u4e4b\u95f4\u7684\u901a\u4fe1\u65b9\u5f0f\u3002<\/p>\n<pre class=\"lang:default decode:true\">&lt;div ng-controller=\"Parent\"&gt;\n  &lt;div ng-controller=\"Child\"&gt;\n    &lt;div ng-controller=\"ChildOfChild\"&gt;\n       &lt;button ng-click=\"someParentFunctionInScope()\"&gt;Do&lt;\/button&gt;\n    &lt;\/div&gt;\n  &lt;\/div&gt;\n&lt;\/div&gt;<\/pre>\n<p><strong>\u4f18\u70b9:<\/strong><\/p>\n<ul>\n<li>\u5bf9\u4e8e\u4ece\u7956\u5148\u5230\u5b50\u5b59\u7684\u6570\u636e\u4f20\u9012\u6548\u679c\u5f88\u597d<\/li>\n<\/ul>\n<p><strong>\u7f3a\u70b9:<\/strong><\/p>\n<ul>\n<li>\u4ece\u5b50\u5b59\u5230\u7956\u5148\u7684\u6570\u636e\u4f20\u9012\u6548\u679c\u4e0d\u597d\uff0c\u5b50 Scope \u7684\u5c5e\u6027\u4f1a\u9690\u85cf\uff08\u8986\u76d6\uff09\u4e86\u7236 Scope \u4e2d\u7684\u540c\u540d\u5c5e\u6027\uff0c\u5bf9\u5b50 Scope \u5c5e\u6027\u7684\u66f4\u6539\u5e76\u4e0d\u66f4\u65b0\u7236 Scope\u540c\u540d\u5c5e\u6027\u7684\u503c\u3002\uff08\u8fd9\u4e2a\u884c\u4e3a\u5b9e\u9645\u4e0a\u4e0d\u662f AngularJS \u7279\u6709\u7684\uff0cJavaScript \u672c\u8eab\u7684\u539f\u578b\u94fe\u5c31\u662f\u8fd9\u6837\u5de5\u4f5c\u7684\u3002\uff09<\/li>\n<li>\u4e0d\u80fd\u8fdb\u884c\u5144\u5f1f\u4f5c\u7528\u57df\u7684\u6570\u636e\u4f20\u9012\uff0c\u9664\u975e\u7528\u4e00\u4e2a\u5171\u540c\u7956\u5148\uff0c\u4f8b\u5982$rootScope<\/li>\n<li>\u8c03\u7528\u7956\u5148\u51fd\u6570\u610f\u5473\u7740\u7956\u5148\u4e0e\u5b50\u5b59\u4e4b\u95f4\u7684\u7d27\u5bc6\u8026\u5408\uff0c\u5f53\u7a0b\u5e8f\u590d\u6742\u5230\u4e00\u5b9a\u7a0b\u5ea6\u65f6\u4fee\u6539\u8d77\u6765\u4f1a\u5bfc\u81f4\u7275\u4e00\u53d1\u52a8\u5168\u8eab\u7684\u60b2\u60e8\u7ed3\u5c40<\/li>\n<\/ul>\n<p><strong>\u9002\u7528\u8303\u56f4:<\/strong><\/p>\n<p>\u4ece\u4e0a\u9762\u7684\u4f18\u7f3a\u70b9\u5206\u6790\u4e2d\u6211\u4eec\u53ef\u4ee5\u770b\u5230\u4f5c\u7528\u57df\u7ee7\u627f\u65b9\u6cd5\u6709\u5f88\u5927\u7684\u5c40\u9650\u6027\u3002\u6545\u800c\u4f5c\u7528\u57df\u7ee7\u627f\u901a\u5e38\u53ea\u7528\u5728\u7b80\u5355\u3001\u5c0f\u578b\u7684\u6a21\u5757\u4e2d\uff0c\u4f8b\u5982directive\u6307\u4ee4\u7684\u4e66\u5199\u4e2d\u3002<\/p>\n<h2>3.\u4f5c\u7528\u57df\u7ee7\u627f+$watch<\/h2>\n<p>\u4e3a\u4e86\u89e3\u51b3\u4f5c\u7528\u57df\u7ee7\u627f\u4e0d\u80fd\u89e3\u51b3\u7684\u4ece\u5b50\u5b59\u5230\u7956\u5148\u7684\u6570\u636e\u4f20\u9012\u95ee\u9898\uff0c\u53ef\u4ee5\u7528$scope.$watch\u51fd\u6570\u6765\u76d1\u89c6\u6570\u636e\u53d8\u5316\u3002<\/p>\n<pre class=\"lang:default decode:true\">\/\/\u7236\u4f5c\u7528\u57df\u76d1\u89c6\u5b50\u4f5c\u7528\u57df\n.controller(\"Parent\", function($scope){\n  $scope.VM = {a: \"a\", b: \"b\"};\n  $scope.$watch(\"VM.a\", function(newVal, oldVal){\n    \/\/ react\n  });\n}\n\n\/\/\u5b50\u4f5c\u7528\u57df\u76d1\u89c6\u7236\u4f5c\u7528\u57df\n.controller(\"child\", function($scope){\n    $scope.$parent.$watch($scope.VM.a, function(){\n       \/\/react\n    });\n}<\/pre>\n<p>angularjs\u7684\u6307\u4ee4\u4e66\u5199\u6a21\u5f0f\u4e2d\uff0c\u8fd8\u6709\u4e00\u79cd\u6307\u5b9a\u6307\u4ee4\u7684scope\u7684\u65b9\u5f0f\uff0c\u672c\u8d28\u4e0a\u4e0e\u6b64\u76f8\u901a\uff0c\u8bf8\u5982\uff1a<\/p>\n<pre>$compileProvider.directive('anrow', function ($compile) {\n\t\treturn {\n\t\t\trequire:    '^angrid',\n\t\t\trestrict:   'E',\n\t\t\ttransclude: true,\n\t\t\tscope:      {\n\t\t\t\tanrowData:     \"=anrowData\",\n\t\t\t\tselectedItems: \"=selects\",\n\t\t\t\tsearchFilter:  \"=searchFilter\"\n\t\t\t},\n\t\t\ttemplate: '',\t\t\n\t\t\treplace:    true,\n                        link: function(scope, element, attrs, angridCtrl){\n                        }\n                 }\n\n});<\/pre>\n<p><strong>\u4f18\u70b9:<\/strong><\/p>\n<ul>\n<li>\u9002\u5408\u7528\u5728\u975econtroller\u751f\u6210\u7684\u5b50\u4f5c\u7528\u57df\u4e2d\uff0c\u4f8b\u5982ng-repeat\u751f\u6210\u7684\u5927\u91cf\u81ea\u4f5c\u7528\u57df\u4e2d<\/li>\n<\/ul>\n<p><strong>\u7f3a\u70b9:<\/strong><\/p>\n<ul>\n<li>\u5bf9\u4e8eone-time events\uff08\u4e00\u6b21\u6027\u4e8b\u4ef6\uff09\u6ca1\u4ec0\u4e48\u6548\u679c<\/li>\n<li>$watch\u51fd\u6570\u7c7b\u4f3ceval\u51fd\u6570\uff0c\u5199\u51fa\u6765\u7684\u4ee3\u7801\u4e0d\u5b9c\u8bfb<\/li>\n<\/ul>\n<p><strong>\u9002\u7528\u8303\u56f4:<\/strong><\/p>\n<p>$watch\u51fd\u6570\u529f\u80fd\u5f88\u5f3a\u5927\uff0c\u914d\u5408$scope.$parent\u548c\u539f\u672c\u7684\u7ee7\u627f\u5173\u7cfb\u53ef\u4ee5\u5b9e\u73b0\u7236\u5b50\u4f5c\u7528\u57df\u7684\u5404\u79cd\u6570\u636e\u4f20\u9012\u3002\u5076\u5c14\u8fd8\u4f1a\u9700\u8981\u4f7f\u7528$scope.$eval\u65b9\u6cd5\u6765\u5c06\u5b57\u7b26\u4e32\u53d8\u4e3a\u5bf9\u8c61\u3002\u8fd9\u79cd\u65b9\u6cd5\u7528\u5728\u6574\u4f53\u67b6\u6784\u7684controller\u4e2d\u6216\u8bb8\u4f1a\u5bfc\u81f4\u4ee3\u7801\u96be\u4ee5\u8bfb\u61c2\uff0c\u4f46\u662f\u7528\u5728\u4e00\u4e9bj\u8f83\u4e3a\u72ec\u7acb\u7684angularjs\u6307\u4ee4\u6216\u63d2\u4ef6\u4e2d\u786e\u662f\u6781\u597d\u7684\u3002\u4f8b\u5982\u6211\u5199\u7684angularjs \u8868\u683c\u63d2\u4ef6<a href=\"https:\/\/github.com\/zhangdiwaa\/anGrid\">angrid<\/a>\u5c31\u662f\u7528\u8fd9\u4e2a\u65b9\u6cd5\u5199\u7684\u3002\u8868\u683c\u4e2d\u5305\u542b\u5927\u91cfng-repeat\u751f\u6210\u7684\u81ea\u4f5c\u7528\u57df\uff0c\u51e0\u4e4e\u90fd\u662f\u7528\u8fd9\u79cd\u65b9\u5f0f\u6765\u5b9e\u73b0\u3002<\/p>\n<h2>4.\u6d88\u606f\u673a\u5236<\/h2>\n<p>\u5f02\u6b65\u56de\u8c03\u54cd\u5e94\u5f0f\u901a\u4fe1\u2014\u4e8b\u4ef6\u673a\u5236\u662fjavascript\u89e3\u51b3\u6a21\u5757\u901a\u4fe1\u7684\u6700\u5e38\u7528\u624b\u6bb5\u3002\u5728angularjs\u4e2d\u6b64\u65b9\u6cd5\u8868\u73b0\u4e3a\u7531$scope\u4e0b\u5b9a\u4e49\u7684\u4e09\u4e2a\u51fd\u6570$broadcast, $emit, $on\u7ec4\u6210\u7684\u4e8b\u4ef6\u96a7\u9053\u901a\u4fe1\u673a\u5236\u3002\u8fd9\u91cc\u6211\u63f4\u5f15 \u7834\u72fc\u7684\u535a\u5ba2 \u00a0<a id=\"cb_post_title_url\" href=\"http:\/\/www.cnblogs.com\/whitewolf\/archive\/2013\/04\/16\/3024843.html\">Angularjs Controller \u95f4\u901a\u4fe1\u673a\u5236<\/a>\u00a0\u6765\u7b80\u5355\u5730\u8bf4\u660e\u8fd9\u4e2a\u65b9\u6cd5\u600e\u4e48\u7528\uff1a<\/p>\n<blockquote>\n<h2>\u00a0Angularjs\u4e3a\u5728scope\u4e2d\u4e3a\u6211\u4eec\u63d0\u4f9b\u4e86\u5192\u6ce1\u548c\u96a7\u9053\u673a\u5236\uff0c$broadcast\u4f1a\u628a\u4e8b\u4ef6\u5e7f\u64ad\u7ed9\u6240\u6709\u5b50controller\uff0c\u800c$emit\u5219\u4f1a\u5c06\u4e8b\u4ef6\u5192\u6ce1\u4f20\u9012\u7ed9\u7236controller\uff0c$on\u5219\u662fangularjs\u7684\u4e8b\u4ef6\u6ce8\u518c\u51fd\u6570\uff0c\u6709\u4e86\u8fd9\u4e00\u4e9b\u6211\u4eec\u5c31\u80fd\u5f88\u5feb\u7684\u4ee5angularjs\u7684\u65b9\u5f0f\u53bb\u89e3\u51b3angularjs controller\u4e4b\u95f4\u7684\u901a\u4fe1\uff0c\u4ee3\u7801\u5982\u4e0b\uff1a<\/h2>\n<pre class=\"lang:default decode:true\">&lt;div ng-app=\"app\" ng-controller=\"parentCtr\"&gt;\n    &lt;div ng-controller=\"childCtr1\"&gt;name :\n        &lt;input ng-model=\"name\" type=\"text\" ng-change=\"change(name);\" \/&gt;\n    &lt;\/div&gt;\n    &lt;div ng-controller=\"childCtr2\"&gt;Ctr1 name:\n        &lt;input ng-model=\"ctr1Name\" \/&gt;\n    &lt;\/div&gt;\n&lt;\/div&gt;<\/pre>\n<p>&nbsp;<\/p>\n<pre class=\"lang:default decode:true\">angular.module(\"app\", []).controller(\"parentCtr\",\nfunction ($scope) {\n    $scope.$on(\"Ctr1NameChange\",\n\n    function (event, msg) {\n        console.log(\"parent\", msg);\n        $scope.$broadcast(\"Ctr1NameChangeFromParrent\", msg);\n    });\n}).controller(\"childCtr1\", function ($scope) {\n    $scope.change = function (name) {\n        console.log(\"childCtr1\", name);\n        $scope.$emit(\"Ctr1NameChange\", name);\n    };\n}).controller(\"childCtr2\", function ($scope) {\n    $scope.$on(\"Ctr1NameChangeFromParrent\",\n\n    function (event, msg) {\n        console.log(\"childCtr2\", msg);\n        $scope.ctr1Name = msg;\n    });\n});<\/pre>\n<p>&nbsp;<\/p>\n<p>\u8fd9\u91ccchildCtr1\u7684name\u6539\u53d8\u4f1a\u4ee5\u5192\u6ce1\u4f20\u9012\u7ed9\u7236controller\uff0c\u800c\u7236controller\u4f1a\u5bf9\u4e8b\u4ef6\u5305\u88c5\u5728\u5e7f\u64ad\u7ed9\u6240\u6709\u5b50controller\uff0c\u800cchildCtr2\u5219\u6ce8\u518c\u4e86change\u4e8b\u4ef6\uff0c\u5e76\u6539\u53d8\u81ea\u5df1\u3002\u6ce8\u610f\u7236controller\u5728\u5e7f\u64ad\u65f6\u5019\u4e00\u5b9a\u8981\u6539\u53d8\u4e8b\u4ef6name\u3002<\/p>\n<p>jsfiddle\u94fe\u63a5\uff1a<a href=\"http:\/\/jsfiddle.net\/whitewolf\/5JBA7\/15\/\">http:\/\/jsfiddle.net\/whitewolf\/5JBA7\/15\/<\/a><\/p><\/blockquote>\n<p><strong>\u4f18\u70b9:<\/strong><\/p>\n<ul>\n<li>\u5bf9\u4e00\u6b21\u6027\u4e8b\u4ef6\u7684\u6548\u679c\u5f88\u597d<\/li>\n<li>\u4e8b\u4ef6\u673a\u5236\u53ef\u4ee5\u6709\u6548\u964d\u4f4econtroller\u4e4b\u95f4\u7684\u8026\u5408\u5ea6<\/li>\n<\/ul>\n<p><strong>\u7f3a\u70b9:<\/strong><\/p>\n<ul>\n<li>\u7531\u4e8eDOM\u6811\u4e8b\u4ef6\u54cd\u5e94\u673a\u5236\u7b49\u539f\u56e0\uff0cangularjs\u91cc\u7684\u4e8b\u4ef6\u673a\u5236\u4e5f\u662f\u91c7\u53d6\u5192\u6ce1+\u5e7f\u64ad\u7684\u65b9\u5f0f\uff0c\u4e0d\u80fd\u50cfC\u8bed\u8a00\u4e2d\u90a3\u6837\u5b9a\u4e49\u4e8b\u4ef6\u89e6\u53d1\u548c\u54cd\u5e94\u69fd\u8fd9\u6837\u7684\u76f4\u63a5\u54cd\u5e94\u5173\u7cfb\u3002\u8fd9\u4e2a\u56e0\u7d20\u76f4\u63a5\u5bfc\u81f4\u5982\u4e0b\u4e24\u4e2a\u95ee\u9898\uff1a<\/li>\n<li>\u4e0d\u80fd\u8fdb\u884c\u5144\u5f1f\u4f5c\u7528\u57df\u7684\u6570\u636e\u4f20\u9012\uff0c\u9664\u975e\u7528\u4e00\u4e2a\u5171\u540c\u7956\u5148\uff0c\u4f8b\u5982$rootScope<\/li>\n<li>\u76f8\u6bd4$emit\u5192\u6ce1\u65b9\u6cd5\uff0c$broadcast\u5e7f\u64ad\u65b9\u6cd5\u8981\u6d88\u8017\u66f4\u591a\u7684\u8d44\u6e90\uff0c\u56e0\u4e3a\u5e7f\u64ad\u4e8b\u4ef6\u4f1a\u6df1\u5165\u5230\u8be5\u4f5c\u7528\u57df\u7684\u6240\u6709\u5b50\u5b59\u4f5c\u7528\u57df\uff0c\u8ddf\u5355\u8def\u5f84\u5192\u6ce1\u7684$emit\u6d88\u8017\u7684\u8d44\u6e90\u5b8c\u5168\u4e0d\u662f\u6570\u91cf\u7ea7\u3002\u6545\u800c\u5bf9\u4e8e\u5305\u542b\u6210\u6570\u5343\u5b50\u4f5c\u7528\u57df\u53c8\u8981\u8ffd\u6c42\u8f83\u9ad8\u6027\u80fd\u7684\u60c5\u51b5\uff0c\u53ef\u80fd\u9700\u8981\u8003\u8651\u4e00\u4e0b\u662f\u5426\u5f03\u7528$broadcast\u65b9\u6cd5\u3002\u8fd9\u91cc\u6709\u4e00\u4e2a\u5bf9\u6bd4\u6d4b\u8bd5\uff0c$rootScope.emit() vs $rootScope.$broadcast, performance tests\u00a0(<a href=\"http:\/\/jsperf.com\/rootscope-emit-vs-rootscope-broadcast\" rel=\"nofollow\">http:\/\/jsperf.com\/rootscope-emit-vs-rootscope-broadcast<\/a>)\u3002<\/li>\n<\/ul>\n<p><strong>\u9002\u7528\u8303\u56f4:<\/strong><\/p>\n<p>\u4e8b\u4ef6\u96a7\u9053\u673a\u5236\u53ef\u4ee5\u89e3\u51b3\u7edd\u5927\u90e8\u5206\u4e8b\u4ef6\u901a\u4fe1\u95ee\u9898\uff0c\u4e5f\u662f\u8fd9\u91cc\u975e\u5e38\u63a8\u8350\u7684\u65b9\u5f0f\u3002<\/p>\n<p>\u4e0d\u8fc7\uff0c\u5f53\u6a21\u5757\u590d\u6742\u5230\u4e00\u5b9a\u7a0b\u5ea6\uff0c\u53ef\u80fd\u5c31\u8981\u63f4\u5f15\u4e00\u4e9b\u8bbe\u8ba1\u6a21\u5f0f\u65b9\u9762\u7684\u77e5\u8bc6\u624d\u80fd\u89e3\u51b3\u95ee\u9898\uff0c\u800c\u4e8b\u4ef6\u96a7\u9053\u673a\u5236\u3001$rootScope\u3001scope\u7ee7\u627f+$watch\u65b9\u5f0f \u90fd\u662f\u6210\u4e3a\u4e86\u5b9e\u73b0\u8bbe\u8ba1\u6a21\u5f0f\u7684\u57fa\u672c\u624b\u6bb5\u3002<\/p>\n<h2>5.\u4e13\u7528service<\/h2>\n<p>\u524d\u6587\u5df2\u7ecf\u63d0\u5230\uff0c\u53ef\u4ee5\u4e13\u95e8\u6784\u5efaservice\u6765\u5904\u7406\u4f5c\u7528\u57df\u95f4\u7684\u901a\u4fe1\u95ee\u9898\u3002\u5982\u679ccontroller\u4e4b\u95f4\u6709\u8f83\u5f3a\u4f9d\u8d56\uff0c\u4f8b\u5982\u90fd\u4f1a\u64cd\u4f5c\u540c\u4e00\u4e2a\u6570\u636e\u96c6\uff0c\u90a3\u4e48\u521b\u5efa\u4e00\u4e2a\u4e13\u95e8\u7684service\u6a21\u5757\u6765\u5904\u7406\u6b64\u7c7b\u4e8b\u52a1\uff0c\u6bd4\u76f4\u63a5\u7528\u4e8b\u4ef6\u96a7\u9053\u673a\u5236\u5728\u903b\u8f91\u4e0a\u66f4\u6e05\u6670\u3002\u4e00\u4e2a\u6700\u7b80\u5355\u7684\u670d\u52a1\u6a21\u5757\u7684\u4f8b\u5b50\u5982\u4e0b\uff1a<\/p>\n<pre class=\"lang:js decode:true\">var myApp = angular.module('myApp', []);\n\nmyApp.factory('Data', function () {\n  return { message: \"I'm data from a service\" };\n});\n\nfunction FirstCtrl($scope, Data) {\n  $scope.data = Data;\n}\n\nfunction SecondCtrl($scope, Data) {\n  $scope.data = Data;\n}<\/pre>\n<p>\u7136\u800c\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u4ec5\u4ec5\u628a\u6570\u636e\u5b58\u53d6\u62bd\u53d6\u51fa\u6765\u662f\u4e0d\u8db3\u591f\u7684\uff0c\u6211\u4eec\u8fd8\u9700\u8981\u89e6\u53d1\u673a\u5236\uff0c\u4ee5\u4fdd\u8bc1ctrl1\u4f7f\u6570\u636e\u53d8\u5316\u540e\uff0cctrl2\u7684\u6570\u636e\u4e5f\u80fd\u8ddf\u7740\u6539\u53d8\u3002\u5bf9\u6b64\u6709\u4e24\u79cd\u529e\u6cd5\uff0c\u5176\u4e00\u662f\u4f7f\u7528\u6d88\u606f\u673a\u5236\uff0c\u5176\u4e8c\u662f\u4f7f\u7528$watch\u65b9\u6cd5\u6765\u68c0\u6d4b\u6570\u636e\u53d8\u5316\u3002<\/p>\n<p>\u4f7f\u7528$watch\u6765\u76d1\u63a7\u6570\u636e\u53d8\u5316\u7684\u4f8b\u5b50\uff0c\u8fd9\u91cc\u6211\u627e\u4e86\u4e00\u4e2a\u6bd4\u8f83\u5178\u578b\u7684\uff0c\u56e0\u4e3a\u6bd4\u8f83\u957f\u6240\u4ee5\u6211\u53ea\u8d34\u94fe\u63a5\uff1ahttp:\/\/jsbin.com\/rifob\/1\/edit?html,js,output<\/p>\n<p>\u4e0b\u9762\u8fd9\u4e2a\u4f8b\u5b50\u5219\u662f\u4f7f\u7528\u4e8b\u4ef6\u901a\u4fe1\u673a\u5236\uff1a\u4ee3\u7801\u6765\u81ea\uff1ahttp:\/\/jsfiddle.net\/simpulton\/XqDxG\/\u3002<\/p>\n<pre class=\"lang:default decode:true\">var myModule = angular.module('myModule', []);\nmyModule.factory('mySharedService', function($rootScope) {\n    var sharedService = {};\n\n    sharedService.message = '';\n\n    sharedService.prepForBroadcast = function(msg) {\n        this.message = msg;\n        this.broadcastItem();\n    };\n\n    sharedService.broadcastItem = function() {\n        $rootScope.$broadcast('handleBroadcast');\n    };\n\n    return sharedService;\n});\n\nfunction ControllerZero($scope, sharedService) {\n    $scope.handleClick = function(msg) {\n        sharedService.prepForBroadcast(msg);\n    };\n\n    $scope.$on('handleBroadcast', function() {\n        $scope.message = sharedService.message;\n    });        \n}\n\nfunction ControllerOne($scope, sharedService) {\n    $scope.$on('handleBroadcast', function() {\n        $scope.message = 'ONE: ' + sharedService.message;\n    });        \n}\n\nfunction ControllerTwo($scope, sharedService) {\n    $scope.$on('handleBroadcast', function() {\n        $scope.message = 'TWO: ' + sharedService.message;\n    });\n}\n\nControllerZero.$inject = ['$scope', 'mySharedService'];        \n\nControllerOne.$inject = ['$scope', 'mySharedService'];\n\nControllerTwo.$inject = ['$scope', 'mySharedService'];<\/pre>\n<p>&nbsp;<\/p>\n<h2>6.\u53d1\u5e03\/\u8ba2\u9605\u6a21\u5f0f<\/h2>\n<p>\u663e\u7136\u7a0b\u5e8f\u5458\u5e76\u4e0d\u4ee5\u4ee5\u4e0a\u51e0\u79cd\u65b9\u5f0f\u6b64\u4e3a\u6ee1\u8db3\u3002\u524d\u6587\u8bb2\u4e8b\u4ef6\u5df2\u7ecf\u63d0\u5230\uff0c\u5f53\u6a21\u5757\u590d\u6742\u5230\u4e00\u5b9a\u7a0b\u5ea6\uff0c\u5982\u679c\u4ec5\u4ec5\u4f7f\u7528\u6d88\u606f\u673a\u5236\uff0c\u540c\u7ea7\u4f5c\u7528\u57df\u7684\u4ea4\u4e92\u90fd\u9700\u8981\u7ecf\u8fc7\u7236\u4f5c\u7528\u57df\u6765\u4f20\u9012\u6d88\u606f\uff0c\u5e76\u4e14\u7ec4\u4ef6\u4e4b\u95f4\u5e7f\u64ad\u6d88\u606f\u610f\u5473\u7740\u5b83\u4eec\u9700\u8981\u591a\u5c11\u77e5\u9053\u4e00\u4e9b\u5176\u5b83\u7ec4\u4ef6\u7f16\u7801\u7684\u7ec6\u8282\uff0c\u8fd9\u6837\u5c31\u9650\u5236\u4e86\u5b83\u4eec\u7684\u6a21\u5757\u5316\u548c\u91cd\u7528\u3002\u8fd9\u4e2a\u65f6\u5019\u5c31\u8981\u5f15\u7528\u4e00\u4e9b\u8bbe\u8ba1\u6a21\u5f0f\u7684\u65b9\u6cd5\u6765\u89e3\u51b3\u95ee\u9898\u3002\u800c\u5b9e\u73b0\u4ed6\u4eec\u7684\u624b\u6bb5\uff0c\u5c31\u662f\u4ee5\u4e0a\u63d0\u5230\u7684 $rootScope\uff0c scope\u7ee7\u627f+$watch, \u6d88\u606f\u673a\u5236 \u548c \u81ea\u5b9a\u4e49service\u3002\u8003\u8651\u6211\u4eec\u6240\u8981\u7684\u9700\u6c42\uff0c\u4e00\u65b9\u9762\u8981\u4fdd\u8bc1\u591a\u4e2acontroller(\u6a21\u5757)\u7684\u6570\u636e\u4e00\u81f4\u6027\uff0c\u4e00\u65b9\u9762\u8fd8\u8981\u4fdd\u8bc1controller(\u6216\u6a21\u5757)\u7684\u6a21\u5757\u5316\u548c\u91cd\u7528\u6027\uff0c\u90a3\u4e48\u5728\u8fd9\u79cd\u573a\u5408\u4f7f\u7528\u89c2\u5bdf\u8005\u6a21\u5f0f\u6216\u5176\u53d8\u79cd\u5c31\u975e\u5e38\u5408\u9002\u3002<\/p>\n<p>\u5728\u6211\u67e5\u9605\u7684\u8d44\u6599\u4e2d\u6709\u5f88\u591a\u56fd\u5916\u7a0b\u5e8f\u5458\u63a8\u8350\u4f7f\u7528\u4f7f\u7528<a href=\"http:\/\/blog.csdn.net\/tjvictor\/article\/details\/5223309\">\u53d1\u5e03\/\u8ba2\u9605\u6a21\u5f0f<\/a>\u3002\u53d1\u5e03\/\u8ba2\u9605\u6a21\u5f0f\u662f\u89c2\u5bdf\u8005\u6a21\u5f0f\u7684\u4e00\u4e2a\u53d8\u79cd\uff0c\u4e5f\u662f\u6d88\u606f\u961f\u5217\u6a21\u5f0f\u7684\u4e00\u7c7b\uff0c\u662f\u89e3\u51b3\u591a\u6a21\u5757\u64cd\u4f5c\u540c\u4e00\u6570\u636e\u96c6\u65f6\u7684\u4e00\u79cd\u5e38\u7528\u65b9\u6848\u3002\u00a0 \u8ba2\u9605\u53d1\u5e03\u6a21\u5f0f\u5b9a\u4e49\u4e86\u4e00\u79cd\u4e00\u5bf9\u591a\u7684\u4f9d\u8d56\u5173\u7cfb\uff0c\u8ba9\u591a\u4e2a\u8ba2\u9605\u8005\u5bf9\u8c61\u540c\u65f6\u76d1\u542c\u67d0\u4e00\u4e2a\u4e3b\u9898\u5bf9\u8c61\u3002\u8fd9\u4e2a\u4e3b\u9898\u5bf9\u8c61\u5728\u81ea\u8eab\u72b6\u6001\u53d8\u5316\u65f6\uff0c\u4f1a\u901a\u77e5\u6240\u6709\u8ba2\u9605\u8005\u5bf9\u8c61\uff0c\u4f7f\u5b83\u4eec\u80fd\u591f\u81ea\u52a8\u66f4\u65b0\u81ea\u5df1\u7684\u72b6\u6001\u3002\u53ef\u4ee5\u6709\u6548\u5730\u5b9e\u73b0\u6a21\u5757\u95f4\u7684\u89e3\u8026\uff0c\u63d0\u9ad8\u53ef\u7ef4\u62a4\u6027\u3002<\/p>\n<p>\u6709\u4e2a\u56fd\u5916\u7a0b\u5e8f\u5458\u5c31\u6b64\u505a\u4e86\u4e00\u4e2ademo\u5e76\u4e14\u5199\u4e86\u7bc7\u535a\u5ba2\u4ecb\u7ecd\u4ed6\u7684\u5b9e\u73b0\uff0c\u73b0\u5728\u4ed6\u7684\u8fd9\u7bc7\u6587\u7ae0\u5df2\u7ecf\u6709\u4e86<a href=\"http:\/\/www.lupaworld.com\/article-243546-1.html\">\u7ffb\u8bd1\u540e\u7684\u7248\u672c<\/a>\u3002\u8fd9\u79cd\u65b9\u6cd5\u4ece\u672c\u8d28\u4e0a\u8bf4\u662f\u540c\u6784\u6784\u5efaservice\u6765\u5904\u7406\u4f5c\u7528\u57df\u95f4\u7684\u901a\u4fe1\u95ee\u9898\uff0c\u8fd8\u662f\u7528$rootScope\u6765\u505a\u9876\u7ea7\u7236\u4f5c\u7528\u57df\uff0c\u5e76\u4e14\u505a\u4e86\u4e8b\u4ef6\u7684\u53d1\u5e03\u548c\u63a5\u6536\u5168\u90e8\u5c01\u88c5\u4e86\u8d77\u6765\u3002\u5927\u5bb6\u6709\u5174\u8da3\u7684\u8bdd\u53ef\u4ee5\u76f4\u63a5\u5230<a href=\"https:\/\/github.com\/lavinjj\/angularjs-pubsub\" rel=\"nofollow\">angularjs-pubsub<\/a>\u00a0\u4e0a\u4e0b\u8f7d\u4ee3\u7801\uff0c\u7814\u7a76\u4ed6\u662f\u600e\u4e48\u505a\u7684\u3002<\/p>\n<p>\u4e0d\u8fc7\u7531\u4e8e\u8fd9\u4f4d\u7a0b\u5e8f\u5458\u5b8c\u6210\u4ed6\u7684demo\u8f83\u65e9\uff0c\u6211\u4e2a\u4eba\u611f\u89c9\u5176\u4e2d\u8fd8\u6709\u5f88\u591a\u53ef\u4ee5\u6539\u8fdb\u7684\u5730\u65b9\u3002\u5982\u4e0b\u4f8b\u662f\u4e00\u4e2a\u8f83\u4e3a\u7b80\u5355\u7684<a href=\"http:\/\/blog.csdn.net\/tjvictor\/article\/details\/5223309\">\u53d1\u5e03\/\u8ba2\u9605\u6a21\u5f0f<\/a>\u7684\u5b9e\u73b0\uff1a\u6b64\u65b9\u6cd5\u901a\u8fc7$rootScope\u5b9a\u4e49\u4e00\u4e2a\u7b80\u5355\u7684\u53d1\u5e03\/\u8ba2\u9605\u8005\u6a21\u5f0f\uff0c\u5e76\u901a\u8fc7\u6d88\u606f\u673a\u5236\u6765\u8fdb\u884c\u53d1\u5e03\u548c\u8ba2\u9605\u3002\u5176\u4e2d\u5c3d\u91cf\u907f\u514d\u4e86$broadcast\u7684\u4f7f\u7528\u3002\u6b64\u4ee3\u7801\u6765\u81ea\uff1ahttp:\/\/jsfiddle.net\/brendanowen\/ADukg\/47\/<\/p>\n<pre class=\"lang:js decode:true\">var myApp = angular.module('myApp', []);\n\nmyApp.service('messageService', ['$rootScope', function($rootScope) {\n\n    return {\n        publish: function(name, parameters) {\n            $rootScope.$emit(name, parameters);\n        },\n        subscribe: function(name, listener) {\n            $rootScope.$on(name, listener);\n        }\n    };\n}]);\n\nmyApp.controller('MyCtrl', ['$scope', 'messageService', function ($scope, messageService) {\n    $scope.showDialog = false;\n    $scope.name = 'Superhero';\n\n    $scope.show = function() {\n        messageService.publish('dialog', {show: true});\n    };\n\n    messageService.subscribe('dialog', function(event, parameters) {\n        $scope.showDialog = parameters.show;\n    });\n\n}]);\n\nmyApp.controller('Dialog', ['$scope', 'messageService', function ($scope, messageService) {\n\n    $scope.hide = function() {\n        messageService.publish('dialog', {show: false});\n    };\n}]);<\/pre>\n<p>\u6b64\u5916\u8fd8\u6709\u4e0d\u4f7f\u7528\u6d88\u606f\u673a\u5236\uff0c\u7eaf\u7cb9\u7528\u81ea\u5b9a\u4e49\u7684\u6d88\u606f\u961f\u5217\u6765\u5b9e\u73b0\u7684\u53d1\u5e03\/\u8ba2\u9605\u6a21\u5f0f\u7684\u4ee3\u7801\u8303\u4f8b\u3002\u6ce8\u610f\u5176\u4e2d\u4f7f\u7528\u4e86jquery\u3002\u4e0b\u9762\u7684\u4ee3\u7801\u6765\u81ea\uff1ahttps:\/\/gist.github.com\/floatingmonkey\/3384419<\/p>\n<pre class=\"lang:js decode:true\">'use strict';\n\n(function() {\n\tvar mod = angular.module(\"App.services\", []);\n\/\/register other services here...\n\t\/* pubsub - based on https:\/\/github.com\/phiggins42\/bloody-jquery-plugins\/blob\/master\/pubsub.js*\/\n\tmod.factory('pubsub', function() {\n\t\tvar cache = {};\n\t\treturn {\n\t\t\tpublish: function(topic, args) {\n\t\t\t\tcache[topic] &amp;&amp; $.each(cache[topic], function() {\n\t\t\t\t\tthis.apply(null, args || []);\n\t\t\t\t});\n\t\t\t},\n\t\t\tsubscribe: function(topic, callback) {\n\t\t\t\tif(!cache[topic]) {\n\t\t\t\t\tcache[topic] = [];\n\t\t\t\t}\n\t\t\t\tcache[topic].push(callback);\n\t\t\t\treturn [topic, callback];\n\t\t\t},\n\t\t\tunsubscribe: function(handle) {\n\t\t\t\tvar t = handle[0];\n\t\t\t\tcache[t] &amp;&amp; d.each(cache[t], function(idx){\n\t\t\t\t\tif(this == handle[1]){\n\t\t\t\t\t\tcache[t].splice(idx, 1);\n\t\t\t\t\t}\n\t\t\t\t});\n\t\t\t}\n\t\t}\n\t});\n\treturn mod;\n})();<\/pre>\n<p>\u6700\u540e\u662f\u6211\u5728\u4e66\u5199\u8fd9\u7bc7\u535a\u5ba2\u524d\uff0c\u5728\u67e5\u9605\u5927\u91cf\u8d44\u6599\u7684\u57fa\u7840\u4e0a\uff0c\u603b\u7ed3\u800c\u6210\u7684\u4e00\u4e2aangularjs\u7684 \u53d1\u5e03\/\u8ba2\u9605 \u6a21\u5f0f \u670d\u52a1\u6a21\u5757\uff0c\u76ee\u524d\u4e0b\u9762\u7684\u4ee3\u7801\u6b63\u5e94\u7528\u4e8e\u6211\u7684\u9879\u76ee\u3002\u6b64\u4ee3\u7801\u8f83\u597d\u5730\u89e3\u51b3\u4e86\u4e09\u4e2a\u95ee\u9898\uff1a<\/p>\n<ul>\n<li>\u7ed3\u6784\u6e05\u6670\u6613\u8bfb\uff0c\u6bd4$watch\u65b9\u5f0f\u5bb9\u6613\u7406\u89e3\u548c\u4f7f\u7528<\/li>\n<li>\u4e0d\u4f7f\u7528$broadcast\uff0c\u53ea\u7528$emit\u6765\u53d1\u5e03\u4e8b\u4ef6\uff0c\u6548\u7387\u8f83\u9ad8\u3002<\/li>\n<li>\u5141\u8bb8\u7528\u6237\u63a7\u5236cache\uff0c\u5728controller\u4e86\u751f\u547d\u5468\u671f\u7ed3\u675f\u540e\u81ea\u52a8\u89e3\u9664$rootscope\u4e0a\u7684\u4e8b\u4ef6\u7ed1\u5b9a\uff0c\u4f4e\u529f\u8017\u65e0\u6c61\u67d3<\/li>\n<\/ul>\n<pre class=\"lang:js decode:true\">define([\n\t'..\/..\/app'\n], function (app) {\n\t\/\/ \u8fd9\u662f\u4e00\u4e2a\u901a\u7528\u7684 \u53d1\u5e03\u8ba2\u9605\u6a21\u5757\n\t\/\/\u53c2\u8003\uff1ahttps:\/\/gist.github.com\/turtlemonvh\/10686980\/038e8b023f32b98325363513bf2a7245470eaf80\n\tapp.factory('pubSubService', ['$rootScope', function ($rootScope) {\n\t\t\/\/ private notification messages\n\t\tvar _DATA_UPDATED_ = '_DATA_UPDATED_';\n\t\t\/*\n\t\t * @name: publish\n\t\t * @description: \u6d88\u606f\u53d1\u5e03\u8005\uff0c\u53ea\u7528$emit\u5192\u6ce1\u8fdb\u884c\u6d88\u606f\u53d1\u5e03\u7684\u4f4e\u80fd\u8017\u65e0\u6c61\u67d3\u65b9\u6cd5\n\t\t * @param: {string=}: msg, \u8981\u53d1\u5e03\u7684\u6d88\u606f\u5173\u952e\u5b57\uff0c\u9ed8\u8ba4\u4e3a'_DATA_UPDATED_'\u6307\u6570\u636e\u66f4\u65b0\n\t\t * @param: {object=}: data\uff0c\u968f\u6d88\u606f\u4e00\u8d77\u4f20\u9001\u7684\u6570\u636e\uff0c\u9ed8\u8ba4\u4e3a\u7a7a\n\t\t * @example:\n\t\t * \t\tpubSubService.publish('config.itemAdded', {'id': getID()});\n\t\t * \t    \u66f4\u4e00\u822c\u7684\u5f62\u5f0f\u662f\uff1a\n\t\t *      pubSubService.publish();\n\t\t *\/\n\t\tvar publish = function (msg, data) {\n\t\t\tmsg = msg || _DATA_UPDATED_;\n\t\t\tdata = data || {};\n\t\t\t$rootScope.$emit(msg, data);\n\t\t};\n\t\t\/*\n\t\t * @name: subscribe\n\t\t * @description: \u6d88\u606f\u8ba2\u9605\u8005\n\t\t * @param: {function}: \u56de\u8c03\u51fd\u6570\uff0c\u5728\u8ba2\u9605\u6d88\u606f\u5230\u6765\u65f6\u6267\u884c\n\t\t * @param: {object=}: \u63a7\u5236\u5668\u4f5c\u7528\u57df\uff0c\u7528\u4ee5\u89e3\u7ed1\u5b9a,\u9ed8\u8ba4\u4e3a\u7a7a\n\t\t * @param: {string=}: \u6d88\u606f\u5173\u952e\u5b57\uff0c\u9ed8\u8ba4\u4e3a'_DATA_UPDATED_'\u6307\u6570\u636e\u66f4\u65b0\n\t\t * @example:\n\t\t * \t\tpubSubService.subscribe(function(event, data) {\n\t\t *\t    $scope.power = data.power;\n\t\t *\t\t    $scope.mass = data.mass;\n\t\t *\t\t},  $scope, 'data_change');\n\t\t *\t\t\u66f4\u4e00\u822c\u7684\u5f62\u5f0f\u662f\uff1a\n\t\t *\t\tpubSubService.subscribe(function(){});\n\t\t *\/\n\t\tvar subscribe = function (func, scope, msg) {\n\t\t\tif (!angular.isFunction(func)) {\n\t\t\t\tconsole.log(\"pubSubService.subscribe need a callback function\");\n\t\t\t\treturn;\n\t\t\t}\n\t\t\tmsg = msg || _DATA_UPDATED_;\n\t\t\tvar unbind = $rootScope.$on(msg, func);\n\t\t\t\/\/\u53ef\u63a7\u7684\u4e8b\u4ef6\u53cd\u7ed1\u5b9a\u673a\u5236\n\t\t\tif (scope) {\n\t\t\t\tscope.$on('$destroy', unbind);\n\t\t\t}\n\t\t};\n\n\t\t\/\/ return the publicly accessible methods\n\t\treturn {\n\t\t\tpublish:        publish,\n\t\t\tsubscribe:      subscribe\n\t\t};\n\t}])\n});<\/pre>\n<p><strong>\u4f18\u70b9:<\/strong><\/p>\n<ul>\n<li>\u6211\u4eec\u53ef\u4ee5\u964d\u4f4e\u7ec4\u4ef6\u4e4b\u95f4\u7684\u8026\u5408\u5ea6\uff0c\u5e76\u5c06\u5b83\u4eec\u7684\u4e4b\u95f4\u901a\u4fe1\u7684\u7ec6\u8282\u5c01\u88c5\u8d77\u6765\u3002\u5728\u4e0d\u5f71\u54cd\u4e3b\u4f53\u529f\u80fd\u7684\u60c5\u51b5\u4e0b\u63d0\u9ad8\u6a21\u5757\u7684\u91cd\u7528\u6027\u3002<\/li>\n<\/ul>\n<p><strong>\u7f3a\u70b9:<\/strong><\/p>\n<ul>\n<li>\u7f3a\u70b9\u662f\u589e\u52a0\u4e86\u4ee3\u7801\u590d\u6742\u5ea6\uff0c\u6240\u4ee5\u4e00\u5b9a\u8981\u5199\u8db3\u591f\u7ed9\u529b\u7684\u63a5\u53e3\u8bf4\u660e\u6587\u6863\uff0c\u5426\u5219\u65f6\u95f4\u4e45\u4e86\u81ea\u5df1\u90fd\u4e0d\u77e5\u9053\u53d1\u751f\u4e86\u4ec0\u4e48\u4e8b\u60c5\u3002<\/li>\n<\/ul>\n<p><strong>\u9002\u7528\u8303\u56f4:<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p>\u8be5\u65b9\u6848\u63a8\u8350\u7ed9\u5185\u90e8\u542b\u6709\u5927\u91cf\u4f5c\u7528\u57df\u901a\u4fe1\uff0c\u5e76\u4e14\u7279\u522b\u5f3a\u8c03\u4ee3\u7801\u91cd\u7528\u6027\u7684\u573a\u5408\u4f7f\u7528\u3002<\/p>\n<p>\u53d1\u5e03\/\u8ba2\u9605\u6a21\u5f0f\u5df2\u7ecf\u80fd\u89e3\u51b3\u5927\u90e8\u5206\u590d\u6742\u591a\u6a21\u5757\u7684\u901a\u4fe1\u95ee\u9898\u4e86\u3002\u4f46\u662f\u5982\u679c\u6a21\u5757\u5f88\u591a\uff0c\u590d\u6742\u5ea6\u7ee7\u7eed\u4e0a\u5347\uff0c\u90a3\u4e48\u4f1a\u9020\u6210\u6d88\u606f\u79cd\u7c7b\u8fc7\u591a\u3002\u8fd9\u79cd\u65f6\u5019\u6709\u5fc5\u8981\u4f7f\u7528\u8d23\u4efb\u94fe\u6a21\u5f0f\u6765\u66ff\u6362\u666e\u901a\u7684\u53d1\u5e03\/\u8ba2\u9605\u6a21\u5f0f\u3002<\/p>\n<p>\u8bbe\u8ba1\u6a21\u5f0f\u7684\u79cd\u7c7b\u6709\u5f88\u591a\uff0c\u4f46\u662f\u5f15\u5165\u524d\u7aef\u8bbe\u8ba1\u7684\u5e76\u4e0d\u591a\u3002\u5f88\u591a\u4eba\u89c9\u5f97\u8bbe\u8ba1\u6a21\u5f0f\u96be\u662f\u56e0\u4e3a\u4e0d\u5b58\u5728\u4e00\u4e2a\u201c\u5b8c\u7f8e\u201d\u7684\u6a21\u5f0f\uff0c\u5fc5\u987b\u6839\u636e\u5b9e\u9645\u60c5\u51b5\u6765\u9009\u7528\u76f8\u5e94\u7684\u6a21\u5f0f\uff0c\u6216\u8005\u8bf4\uff0c\u5b8c\u7f8e\u662f\u4e0d\u65ad\u9002\u5e94\u65b0\u60c5\u51b5\u7684\u80fd\u529b\u3002\u8fd9\u79cd\u968f\u673a\u5e94\u53d8\u7684\u80fd\u529b\u624d\u662f\u771f\u6b63\u8003\u9a8c\u4ee3\u7801\u8bbe\u8ba1\u8005\u7684\u95ee\u9898\u3002<\/p>\n<p>&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;<\/p>\n<p>\u66f4\u591a\u5173\u4e8econtroller\u95f4\u901a\u4fe1\u7684\u8ba8\u8bba\u8bf7\u89c1\uff1a<\/p>\n<p>http:\/\/stackoverflow.com\/questions\/11252780\/whats-the-correct-way-to-communicate-between-controllers-in-angularjs\/19498009#19498009<\/p>\n<p>http:\/\/stackoverflow.com\/questions\/26751889\/communication-between-controllers-in-angular<\/p>\n","protected":false},"excerpt":{"rendered":"<p>&nbsp; \u6700\u8fd1\u5728\u505ad3js + an<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[6],"tags":[16,37],"class_list":["post-1103","post","type-post","status-publish","format-standard","hentry","category-web-frontend","tag-angularjs","tag-javascript"],"_links":{"self":[{"href":"https:\/\/www.divis.cn\/index.php\/wp-json\/wp\/v2\/posts\/1103","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.divis.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.divis.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.divis.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.divis.cn\/index.php\/wp-json\/wp\/v2\/comments?post=1103"}],"version-history":[{"count":0,"href":"https:\/\/www.divis.cn\/index.php\/wp-json\/wp\/v2\/posts\/1103\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.divis.cn\/index.php\/wp-json\/wp\/v2\/media?parent=1103"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.divis.cn\/index.php\/wp-json\/wp\/v2\/categories?post=1103"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.divis.cn\/index.php\/wp-json\/wp\/v2\/tags?post=1103"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}