【问题标题】:angularjs directive: how communicate between link and controller?angularjs 指令:链接和控制器之间如何通信?
【发布时间】:2015-09-15 02:13:36
【问题描述】:

我有一个指令,我需要在指令控制器中访问其“config”属性值。 由于首先执行控制器构造函数,因此可以从控制器到链接进行通信,但反之则不行。 实现这一目标的最佳方法应该是什么? 我考虑了以下方法

1) 将变量添加到作用域- 在我看来,这会污染范围,使变量在共享范围的任何其他地方都可以访问。

2)使用$broadcast 又是和上面一样的问题

3) 在控制器的this 上传递一个回调函数,并以config 作为参数从链接函数调用它

4)通过服务传递值-在我的情况下,我有多个此类指令需要通过该服务传递日期

或者有没有更好的方法让我错过了这样做?

module.directive('myDirective',function(){

return{
 restrict:'E',
 templateUrl:'path/to/html',
 link:function(scope,iElement,iAttrs,controller){

  var config=iAttrs.config;
//How to access this value inside the directive controller?

},
controller:function($scope){
//the directive attribute 'config' is required here for some larger computations which are not
//manipulating the DOM and hence should be seperated from the link function
})

【问题讨论】:

  • 您是否尝试在控制器中注入 $attrs?
  • 任何描述其用法的链接将不胜感激。文档对 $attrs 不是很清楚

标签: javascript angularjs angularjs-directive dom-manipulation


【解决方案1】:

在那里,您可以使用隔离范围概念,您可以在控制器内部创建隔离范围,并且在原型上不会从其父范围继承。为此,您需要在指令选项中使用scope: { ... }。有三个选项可以通过属性在指令内传递范围值

  1. @ : 一种方式绑定
  2. = : 双向绑定
  3. &:表达式

在您的情况下,前两种情况会很好,这取决于您需要使用哪一种。如果您只想在这种情况下将范围变量的值传递给指令,您可以使用第一种方法,即@ 单向绑定。

如果你想更新指令和控制器中的变量,即只有双向绑定,那么你需要使用=

我认为= 适合你,所以你应该选择=

标记

<my-directive config="config"></my-directive>

指令

app.directive('myDirective', function() {
  return {
    restrict: 'E',
    scope: {
      config: '='
    },
    templateUrl: 'path/to/abc.html',
    link: function(scope, iElement, iAttrs, controller) {
      //here it will be access as scope.config
      console.log(scope.config);
    },
    controller: function($scope) {
      console.log($scope.config); //here also it would be available inisde scope
      //you could put a watch to detect a changes on config
    }
  }
});

Demo Plunkr

更新

由于config 的值是从属性中提供的,带有{{}} 之类的表达式,因此我们可以通过将[**$observe**][2] 放在$attrs 上来获得控制器内部的这些更改。为此,您需要在控制器上注入 $attrs 依赖项,这将为您提供指令元素上可用的所有属性集合。在同一个$attrs 对象上,我们将放置$observe,它的工作方式与$watch 的工作方式相同,后者进行脏检查& 如果值发生变化,它会触发手表。

指令

app.directive('myDirective', function() {
  return {
    restrict: 'E',
    templateUrl: 'path/to/abc.html',
    link: function(scope, iElement, iAttrs, controller) {
      //here it will be access as scope.config
      console.log(scope.config);
    },
    controller: function($scope,$attrs) {
      //you could put a watch to detect a changes on config
      $attrs.$observe('config', function(newV){
        console.log(newV);
      })
    }
  }
});

Updated Demo

【讨论】:

  • 我不能为此指令创建一个隔离作用域,因为它会使我无法直接使用父作用域变量。正如我所说,我有一系列这样的指令需要共享一个公共作用域。
  • @Abdul23 给我几分钟我将创建一个没有孤立范围的代码..首先回答我的一些问题..你有config 信息存储在scope 变量中吗?跨度>
  • 是的,我在父作用域中可以使用它,但是使用= 将它作为作用域变量属性传递意味着我必须在我想要避免的指令中为它创建一个隔离作用域
  • @Abdul23 那么为什么不直接从指令控制器(如$scope.config)访问控制器内部的值如果您的父作用域在$scope.config 中有配置值
  • 这是一个复杂的情况。出于某些原因,我希望我的指令像 &lt;my-directive config="expression to be parsed to find the data"&gt;&lt;/my-directive&gt; 那样使用,而不是像 &lt;my-directive&gt;&lt;/my-directive&gt; 那样使用。
猜你喜欢
  • 1970-01-01
  • 2017-06-19
  • 1970-01-01
  • 2014-10-06
  • 1970-01-01
  • 2015-05-05
  • 1970-01-01
  • 1970-01-01
  • 2014-10-10
相关资源
最近更新 更多