javascript - 使用函数绑定(bind)将值从一个指令传递到另一个指令 '&'

标签 javascript html angularjs angularjs-directive

我想将一个值从父指令传递到子指令。在这种情况下,父指令有一个函数,需要在单击子指令的 div 时执行,并将 ng-model 值从子指令传递到父指令的调用函数。

http://jsfiddle.net/Lvc0u55v/12543/

var myApp = angular.module('myApp', []);
myApp.directive('parentDirective', function() {
  return {
    restrict: 'EA',
    transclude: true,
    template: '<div>Parent directive</div>',
    link: function(scope) {
      scope.someFunc = function(value) {
        alert(value);
      }
    }
  }
});
myApp.directive('childDirective', function() {
  return {
    restrict: 'EA',
    template: '<input type="text" ng-model="data.name"><button ng-click="issue(data.name)">Child Directive</button>',
    scope: {
      issue: '&'
    }
  }
});
<div ng-app="myApp">
  <parent-directive ng-transclude>
    <child-directive issue="someFunc()"></child-directive>
  </parent-directive>
</div>

JSfiddle link

最佳答案

我已经更新了您的JSFiddle使用正确的解决方案。

在父指令中绑定(bind)函数时需要指定参数。

<child-directive issue="someFunc(value)"></child-directive>

并传递一个带有这些参数的对象作为子指令中的键。

<button ng-click="issue({ value: data.name })">Child Directive</button>

关于javascript - 使用函数绑定(bind)将值从一个指令传递到另一个指令 '&',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40794042/

相关文章:

javascript - 黑莓上下文菜单关闭事件

javascript - 如何创建一年中每天增加 1/365 部分的圆轮廓?

javascript - Angular : How can I stop my $http calls from blocking the UI?

javascript - Angular 将特定数据检索到 $scope 变量中不起作用

javascript - 如何将动态 id 从 ajax 数据传递到 Modal

c# - 从 CodeBehind 调用 JavaScript 函数

php - 运行针对地理的 js 文件

javascript - 根据存储在本地存储中的复选框是否处于事件/选中状态来更改 CSS

html - :last-child is not working in nested divs

javascript - 如何从MVC Controller 向AngularJS Controller 返回消息