从父指令更改子指令的属性

时间:2015-03-04 16:12:06

标签: angularjs angularjs-directive

我有一个表单指令,需要与form-sections指令通信,告诉它更改布局。更改由form-header指令触发。

<form>
  <form-header></form-header>
  <form-sections></form-sections>
</form>

我在form-header中注入了form指令的控制器,这样头文件就可以调用changeLayout函数。

表格指示:

compile: function(scope, element , attrs) {
    element.find('form-sections').attr('current-view', 'tabs'); // initial layout
},
controller: function($scope, $element, $attrs) {                
  this.changeLayout = function(layout) {
    $element.find('form-sections').attr('current-view', layout);
  };
}

form-sections指令:

link: function (scope, element, attrs) {
  attrs.$observe('currentView', function(currentView) {
    console.log('observe:'currentView);
  })
}

$ observe只能通过表单指令的编译功能触发一次,而不是由它的控制器触发。我尝试使用form-section指令中的隔离范围并使用$ watch,但没有任何成功..

一如既往地谢谢!

0 个答案:

没有答案