简体   繁体   English

嵌套自定义指令的角度调用控制器功能

[英]Angular call controller function from nested custom directive

I could use some help. 我可以帮忙。 I have a controller containing a function called "loadInformation()". 我有一个包含名为“ loadInformation()”的函数的控制器。 Inside this controller I use a service which is doing some DOM-Manipulation using custom directives. 在此控制器内部,我使用的服务正在使用自定义指令进行一些DOM操纵。 These are the directives: 这些是指令:

First custom Directive: 第一个自定义指令:

angular.module('...').directive('ngInputString', function () {
return {
restrict: 'AE',
replace: 'true',
scope: {
  savedInformation: '=',
  type: '@',
  values: '='
},
templateUrl: 'directives/templates/inputString.html',
link: function (scope, element, attrs) {
  scope.filterOb = {ttype: scope.type};
}
}
});

HTML file: HTML档案:

<div>
<input ttype="{{type}}" type="text" placeholder="{{param.name}}"         value='{{param.value}}'
     class='clean-input form-control'/>
 <ng-saved-information type="STRING" saved-information="savedInformation"></ng-saved-information>
 </div>

Nested custom directive: 嵌套的自定义指令:

angular.module('semtrosApp').directive('ngSavedInformation', function    () {
return {
restrict: 'AE',
replace: 'true',
scope: {
  savedInformation: '=',
  type: '@'
},
template: '<ul><li ng-repeat="information in savedInformation |    filter:filterOb">{{information.value}}<button type="button" ng-click="..?..">Use Information</button></li></ul>',
link: function (scope, elem, attrs) {
  scope.filterOb = {ttype: scope.type};
}
}
});

When I dont use nested directives, it works just fine with that piece of code: 当我不使用嵌套指令时,它与那段代码配合就可以了:

elem.bind('click', function() {
    scope.$apply(function() {
      scope.loadInformation();
    });
  });

But when they are nested, the second custom diretive is just looking inside the scope of the parent directive. 但是,当它们嵌套时,第二个自定义指令只是在父指令的范围内查找。 Any idea how to pass through the function call? 知道如何通过函数调用吗?

It looks like the ngInputString directive already takes in some data and passes it down to ngSavedInformation . 看起来ngInputString指令已经接收了一些数据,并将其传递给ngSavedInformation Why not have it take in a loadInformation callback as well and pass it down? 为什么还不接受loadInformation回调并将其传递下去呢?

angular.module('...').directive('ngInputString', function () {
  return {
    scope: {
      savedInformation: '=',
      type: '@',
      values: '=',
      loadInformation: '&'
    },
    // etc
  }
});

 <ng-saved-information type="STRING" saved-information="savedInformation" load-information="loadInformation()"></ng-saved-information>

 angular.module('semtrosApp').directive('ngSavedInformation', function () {
   return {
     scope: {
       savedInformation: '=',
       type: '@',
       loadInformation: '&'
     },
     // etc
   }
 });

Also, instead of manually creating a click handler, you could do the following in the template: 另外,您可以在模板中执行以下操作,而不是手动创建点击处理程序:

ng-click="loadInformation()"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM