如何在指令 link 函数中使用带参数的“&”调用控制器函数?

How to call a controller function, inside a directive link function using '&' with a parameter?

我试图将一个参数传递给一个函数,在指令 link 函数内部,类似于这个问题: Angular: calling controller function inside a directive link function using &

但是,虽然我看到了一些示例,例如:Passing Parameters from a Directive to a function 并且我看到在指令中的 link 中设置了一个参数值。但是,我还没有看到您将原语传递给指令的任何地方,例如将数字作为参数传递给将其传递给控制器​​函数的指令。

我已经尝试了很多东西,但还没有弄清楚语法。

HTML 代码:

<!DOCTYPE html>
<html>

  <head>
    <script data-require="angular.js@1.4.2" data-semver="1.4.2" src="https://code.angularjs.org/1.4.2/angular.js"></script>
    <link rel="stylesheet" href="style.css" />
    <script src="script.js"></script>
  </head>

  <body>
    <div id="app" ng-app="app">
    <div ng-controller="mainCtrl">
          <my-directive ctrl-fn="ctrlFn(count)"></my-directive> 
        </div>
      </div>
  </body>

</html>

SCRIPT.JS

var app = angular.module('app', []);

app.controller("mainCtrl", function($scope) {
  $scope.count = 0;
  $scope.ctrlFn = function() {
      $scope.count = 0;
      console.log('In mainCtrl ctrlFn!');
      $scope.count += count;
     // console.log("count is: " + JSON.stringify($scope.count));
    //Call service here
  };  
})


.directive('myDirective', function() {
  return {
    restrict: 'E',
    scope: {
      'count' : '&',
      'ctrlFn' : '&'
    },
    template: "<div><button ng-click='ctrlFn({count: 10})'>Click Here</button></div>",
    link: function(scope, element, attributes) {
      var count = null;
      scope.ctrlFn = scope.ctrlFn({count: count});
      //scope.text = scope.fn({ count: 0 });
    }
  };
});

我的笨蛋来了:http://plnkr.co/edit/6uDntNeqe0g343PmeCED?p=preview

在此用例中可以将基元作为参数传入吗?如果是这样,我在这里缺少什么?

后果:

如果有人正在寻找这种语法:angularjs 文档中的 ctrlFn({count: 10}),它在自定义指令下提到:

Often it's desirable to pass data from the isolate scope via an expression to the parent scope, this can be done by passing a map of local variable names and values into the expression wrapper function. For example, the hideDialog function takes a message to display when the dialog is hidden. This is specified in the directive by calling close({message: 'closing for now'}). Then the local variable message will be available within the on-close expression.

你犯了两个错误:

  • scope.ctrlFn = scope.ctrlFn({count: count}); - 此行覆盖传递给函数的引用并将其设置为此函数返回的值(在本例中为 undefined

  • 要将 count 值传递给您的指令,您应该使用 = 而不是 &

下面是简化示例的代码。

script.js:

var app = angular.module('app', []);

app.controller("mainCtrl", function($scope) {
  $scope.count = 0;
  $scope.ctrlFn = function(count) {
      console.log(count)
      console.log('In mainCtrl ctrlFn!', count);
      $scope.count += count;
    //Call service here
  };  
})


.directive('myDirective', function() {
  return {
    restrict: 'E',
    scope: {
      'count' : '=',
      'ctrlFn' : '&'
    },
    template: "<div><button ng-click='ctrlFn({ count: 100 })'>Click Here</button></div>"
  };
})

index.html:

<!DOCTYPE html>
<html>

  <head>
    <script data-require="angular.js@1.4.2" data-semver="1.4.2" src="https://code.angularjs.org/1.4.2/angular.js"></script>
    <link rel="stylesheet" href="style.css" />
    <script src="script.js"></script>
  </head>

  <body>
    <div id="app" ng-app="app">
    <div ng-controller="mainCtrl"> {{count}}
          <my-directive ctrl-fn="ctrlFn(count)"></my-directive> 
        </div>
      </div>
  </body>

</html>