到目前为止,我已经看到了许多问题的解决方案.最简单的是,当然,对$emit一个事件中$rootScope作为事件总线例如(https://github.com/btilford/anti-patterns/blob/master/angular/Angular.md)
angular.module('myModule').directive('directiveA', function($rootScope) {
return {
link : function($scope, $element) {
$element.on('click', function(event) {
$rootScope.$emit('directiveA:clicked', event);
});
}
}
});
angular.module('myModule').directive('directiveB', function() {
return {
link : function($scope, $element) {
$rootScope.on('directiveA:clicked', function(event) {
console.log('received click event from directiveA');
});
}
}
});
Run Code Online (Sandbox Code Playgroud)
另一个是使用中介或pubsub功能/封闭范围声明服务,例如(在多个控制器和指令之间进行通信.)
module.factory('MessageService',
function() {
var MessageService = {};
var listeners = {};
var count = 0;
MessageService.registerListener = function(listener) {
listeners[count] = listener;
count++;
return (function(currentCount) {
return function() { …Run Code Online (Sandbox Code Playgroud) javascript design-patterns mediator publish-subscribe angularjs
整合angularjs到HTML的方式推荐看起来像:
<!doctype html>
<html xmlns:ng="http://angularjs.org" ng-app>
<body>
...
<script src="angular.js">
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
问题是:没有一本教科书使用xmlns:ng="http://angularjs.org"线.哪个重要或有意义?该行应该与每个模块声明一起使用吗?
举个例子:
(function() {
angular.module('Base', []).controller('BaseController', function($scope) {
$scope.mixin1 = function() {};
})
})();
Run Code Online (Sandbox Code Playgroud)
封装angularjs模块有什么意义?我认为默认情况下是这样。