在 AngularJS 中使用 $broadcast 和 Service 将数据传递给指令

Passign data to directive with $broadcast and Service in AngularJS

我正在迁移到基于 Angular 的前端。我在 JavaScript 中有一个函数可以将一些数据传递给指令,经过一些研究发现使用 Service$broadcast 可能是一个很好的解决方案。但对我不起作用...

这是我的代码:

var app = angular.module('app', []);
app.factory('broadcastService', ['$rootScope',
  function($rootScope) {
    var factory = {};
    factory.sendAjaxResut = function(name, obj) {
      console.log(' -- $broadcast ');
      $rootScope.$broadcast(name, obj);
    }
    return factory;
  }
]);

app.directive("bill", [directive]);
function directive() {
  return {
    restrict: "E",
    link: function($scope) {
      $scope.$on("b1", function(e, a) {
        console.log('-- directive')
      });
    }
  }
}

将数据传递给服务的代码:

function _ajaxCUSTOMER(e) {
  angular
    .injector(['ng' ,'app']) 
    .get("broadcastService")
    .sendAjaxResut("b1", e);
}

<button onclick="_ajaxCUSTOMER('foo')" >Send</button>

问题 1:.injector(['ng' ,'app'])

中的 ng 是什么

问题2:此时控制台只显示-- $broadcast。我的代码无法在指令

中捕获事件的问题是什么

jsfiddle

您可以试试这个解决方案:

<button onclick="_ajaxCUSTOMER('foo', 'e')" >Send</button>
function _ajaxCUSTOMER(name,obj) {
    angular.element(document).find('body').injector().get('broadcastService').sendAjaxResut(name, obj);
      console.log('ok');
  }

  myApp.factory('broadcastService', ['$rootScope',
      function($rootScope) {
        console.log('broadcastService');
        var factory = {};
        factory.sendAjaxResut = function(name, obj) {
          console.log(' -- $broadcast ');
          $rootScope.$broadcast('newEvent', name, obj);
        }
        return factory;
      }
    ]);

    myApp.directive('bill', function () {
    return {
        restrict: 'EAC',
        controller: function($scope) {},
        link: function(scope, element, attrs) {
            scope.$on("newEvent", function(event, data, name, obj) {
              console.log('-- directive')
            });
        }
    };
});

您需要在 html.

中定义 controller

jsfiddle.

上的实例

var app = angular.module('app', [])
  .controller('ExampleController', function($scope, broadcastService) {
  });
app.factory('broadcastService', ['$rootScope',
  function($rootScope) {
    var factory = {};
    factory.sendAjaxResut = function(name, obj) {
      console.log(' -- $broadcast ');
      $rootScope.$broadcast(name, obj);
    }
    return factory;
  }
]);

app.directive("bill", function($rootScope) {
  return {
    restrict: "E",
    template:'<div>My bill</div>',
    link: function($scope) {
      $rootScope.$on("b1", function(e, a) {
        console.log('-- directive',a)
      });
    }
  }
});


function _ajaxCUSTOMER1(e) {
angular.element(document).find('body').injector().get('broadcastService').sendAjaxResut('b1', e);
}
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<body id="app" ng-app="app">
  <div ng-controller="ExampleController">
    <button onclick="_ajaxCUSTOMER1('5')">
      Click Here to send 5
    </button>
    <bill>
    </bill>
  </div>
</body>

您的指令未获得 $broadcast,因为您正在创建一个带有新 $rootScope 的新注入器。而是使用您应用的注入器。

function _ajaxCUSTOMER1(e) {
  var rawElem = document.getElementById("app");
  var elem = angular.element(rawElem);
  var _injector = elem.injector();
  var _broadcastService = _injector.get("broadcastService");
  _broadcastService.sendAjaxResut("b1",e);
}

此示例查找您应用的元素并使用 angular.element 检索其注入器。

working JSFiddle.