小编Sup*_*aca的帖子

角度模块中的全局通信:事件总线或中介模式/服务

到目前为止,我已经看到了许多问题的解决方案.最简单的是,当然,对$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

10
推荐指数
2
解决办法
9067
查看次数

第xmlns:ng ="http://angularjs.org"行在哪里工作/需要

整合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"线.哪个重要或有意义?该行应该与每个模块声明一起使用吗?

javascript angularjs

5
推荐指数
1
解决办法
2142
查看次数

将模块隐藏在闭包中的原因是什么?

举个例子:

(function() {
  angular.module('Base', []).controller('BaseController', function($scope) {
    $scope.mixin1 = function() {};
  })
})();
Run Code Online (Sandbox Code Playgroud)

封装angularjs模块有什么意义?我认为默认情况下是这样。

javascript module angularjs angularjs-scope

4
推荐指数
1
解决办法
352
查看次数