【问题标题】:Angular : is it possible to pass an array or multiple strings to the $on event listener so as not to have multiple lines of listeners?Angular:是否可以将数组或多个字符串传递给 $on 事件侦听器,以免有多行侦听器?
【发布时间】:2016-07-01 16:33:42
【问题描述】:

我有类似的东西:

$scope.$on(config.SOME_CONSTANT, ()=> {
    activate();
    // plus a bunch of instantiations
    $scope.$digest();
});

$scope.$on(config.SOME_OTHER_CONSTANT, ()=> {
    activate();
    $scope.$digest();
});

是否可以将它们合并为 1 个听众以便更干?角度文档说它只需要一个字符串 arg ......我有一些有条件的东西,我希望在事件发生的两种情况下都发生。有什么想法吗?

【问题讨论】:

    标签: javascript angularjs model-view-controller controller event-listener


    【解决方案1】:

    您可以通过循环实现它:) 工作JsFiddle Link

    angular.forEach(['one', 'two', 'three'], function(value) {
      $scope.$on(value, function(event) {
        console.log(event.name);
      });
    });
    

    【讨论】:

      【解决方案2】:

      您可以传递一个对象,例如:

      var myApp = { 'id': "myApplicationId", 'name': "myApplicationName"}
      

      【讨论】:

        猜你喜欢
        • 2015-12-07
        • 2012-11-11
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-06-27
        • 2014-02-15
        • 2023-04-06
        • 2018-04-04
        相关资源
        最近更新 更多