1

我正在迁移到基于 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>

ng问题1:里面有什么.injector(['ng' ,'app'])

问题2:此时控制台只显示-- $broadcast。我的代码无法在指令中捕获事件的问题是什么

jsfiddle

4

3 回答 3

1

您的指令没有获得 $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检索其注入器。

工作的JSFiddle

于 2016-02-05T12:47:19.080 回答
0

您需要html.

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>

于 2016-02-05T11:14:36.187 回答
0

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

<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')
            });
        }
    };
});
于 2016-02-05T11:11:17.107 回答