如何将 AngularJS 指令中的值传递给父控制器中的函数

How to pass a value from an AngularJS directive to a function in parent controller

我正在使用 Anguljarjs 1.4x。我正在尝试将值从外部函数传递回指令。我以前做过一些类似的事情,但由于某种原因,函数参数没有返回到我的控制器。

这是一些有效的 plunker 代码,我必须将外部函数传递给指令。我现在要做的是添加一个文本参数值。

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">
          <my-directive ctrl-fn="ctrlFn()"></my-directive> 
        </div>
      </div>

  </body>

</html>

myPage.html

<div>
  <button ng-click='ctrlFn()'>Click Here</button>
</div>

script.js

var app = angular.module('app', []);
app.controller('mainCtrl', function($scope){
  $scope.count = 0;
  $scope.ctrlFn = function() {
      console.log('In mainCtrl ctrlFn!');
      $scope.count += 10;
      console.log("count is: " + JSON.stringify($scope.count));
  };  
});

app.directive('myDirective', function() {
  return {
    restrict: 'E',
    scope: {
      'ctrlFn' : '&'
    },
   // template: "<div><button ng-click='ctrlFn()'>Click Here</button></div>",
   templateUrl: "./myPage.html",
    link: function(scope, element, attributes) {
      scope.ctrlFn();
      console.log("In link!");
    }
  };
});

当应用程序运行时,我得到这个: 在 mainCtrl ctrlFn!

计数为:10

在link!

点击按钮后,我得到了这个: 在 mainCtrl ctrlFn!

计数为:20

正如我所说,这一切都有效,直到我添加了一个参数。我想要做的就是添加一个文本参数,但是当我在 myPage.html 中尝试此操作时出现错误:

Error: cannot use 'in' operator to search for 'ctrlFn' in 'hello'

 <button ng-click='ctrlFn('hello')'>Click Here</button>

我在我的 ng-click 函数中添加了一个文本值,然后在我列出该函数的其他地方创建了一个参数。

关于如何做到这一点有什么想法吗?

在指令模板中使用:

template: `
    <button ng-click="ctrlFn({$event: 'hello'})">
       Click Here
    </button>
`,

这样使用指令:

<my-directive ctrl-fn="ctrlFn($event)"></my-directive> 

DEMO on PLNKR

var app = angular.module('app', []);
app.controller('mainCtrl', function($scope){
  $scope.count = 0;
  $scope.ctrlFn = function(value) {
      $scope.count += 10;
      $scope.message = value;
  };  
});

app.directive('myDirective', function() {
  return {
    restrict: 'E',
    scope: {
      'ctrlFn' : '&'
    },
   template: `
      <div>
        <button ng-click="ctrlFn({$event:'hello'})">
          Click Here
        </button>
      </div>
    `,
    link: function(scope, element, attributes) {
      scope.ctrlFn();
    }
  };
});
<script src="//unpkg.com/angular@1.4/angular.js"></script>
<body ng-app="app" ng-controller="mainCtrl">
      <my-directive ctrl-fn="ctrlFn($event)"></my-directive> 
      message={{message}}<br>
      count={{count}}
</body>


用表达式("&")绑定,定义属性:

<my-directive my-func="parentFn($event)">
</my-directive>

使用定义为本地的 $event 参数调用函数:

export default function myDirective() {
'ngInject';
    return {
        restrict: 'A',
        scope: {
            'myFunc': '&'
        },
        templateUrl: "./myPage.html",
        controller: myCtrl
    };

    function myCtrl($scope) {
        'ngInject';
        console.log("In myCtrl!");
        var answer = $scope.myFunc({$event: "Hello"});
        console.log(answer);
    }
}

这是您的代码的有效插件。

https://next.plnkr.co/edit/TWQEUUWq6h55uOIXDbuR

基本上,您需要从指令中删除括号

<my-directive ctrl-fn="ctrlFn"></my-directive>   

然后在调用之前解包函数(在指令中)。

scope.ctrlFn = scope.ctrlFn();

此答案提供了有关展开函数的更多信息。