了解孤立范围内的角度和山雀()()

Understand the angular & in isolated scope and tits ()()

本文关键字:范围内 了解      更新时间:2023-09-26

我在徘徊为什么当我使用"&"将方法从外部作用域传递到具有隔离作用域的指令时,我需要在指令中用双括号 ()() 调用此方法。

看起来该函数是用callThat()返回的,然后我需要用另一个()调用它。

第一个函数看起来像这样:

function (b){return t(a,b)}

一些 html:

<div class="jumbotron">
  <div class="row" ng-controller='myController'>
    <my-directive call-that="myPassedMethod"></my-directive>
  </div>
</div>

我的控制器

app.controller('myController', function($scope){
  $scope.myPassedMethod = function(){
    console.log('War, war never changes');
  }
});

我有我的指示:

app.directive('myDirective', function() {
  return {
    restrict: 'E',
    scope: {
      callThat : '&'
    },
    templateUrl: 'something_irrelevant.html',
    controller: function ($scope) {
      $scope.calledFromIrrelevantTemplate = function () {
        $scope.callThat()(); // why i need ()(); here?
      };
    }
  }
});

有人可以解释一下我的这种结构吗?(我没有JS开发经验,请使用简单的语言)。

您需要双精度函数调用的原因是因为您像这样传递函数:

<my-directive call-that="myPassedMethod"></my-directive>

您应该将其更改为:

<my-directive call-that="myPassedMethod()"></my-directive>

演示:http://plnkr.co/edit/xJnEEAGfrBXl5NXIIrZs?p=preview