0
我想要动态地编译组件以将其插入到特定的DOM元素(DOM也是由第三方库动态创建的)。 所以,我用$compile
,$scope
。
https://jsbin.com/gutekat/edit?html,js,console,output
// ListController $postLink life cycle hook
function $postLink() {
...
$timeout(function() {
ctrl.items.splice(0, 1);
$log.debug('First item of array is removed');
$log.debug(ctrl.items);
}, 2000);
}
但低于$onChanges
生命周期钩在ListItemController
不执行。
// ListItemController $onChanges life cycle hook
function $onChanges(changes) {
if (!changes.item.isFirstChange()) {
$log.debug(changes); // Not executed
}
}
我想这angular.merge
通过控制器实例初始化item
之前ListItemController
是一个重要原因。
var itemScope = $scope.$new(true, $scope);
itemScope = angular.merge(itemScope, {
$ctrl: {
item: item
}
});
$ onChanges只听你把绑定的属性,因为'item'不绑定,它的任何变化都不会触发$ onChanges功能,你应该使用$来代替手表。 –
@AnonyC然后,如何在'ListController'中编译时在'listItem'组件绑定中正确传递'item'对象? – taehwanno