我创建了一个角度指令,该指令将回调函数作为属性接收。
<my-dir ng-model="foo.bar" on-change="func(data)"></my-dir>
在指令的定义中,我将该属性分配如下
scope: {onChange: '&onChange'}
现在,在链接函数中,如果我调用scope.onChange({data:23});一切正常。但是,如果我在$ watch函数中进行相同的调用,它将失败。
scope.$watch("stepId", function(newValue, oldValue) {
scope.onChange({data: 23}); //why it does NOT work ????
})
最佳答案
也许与其看“ stepId”,不如看一个函数返回的值。
scope.$watch( function(){ return scope.stepId; }, function( newValue, oldValue){
if( typeof newValue == 'undefined' )
return;
scope.onChange( {data: scope.stepId});
});
您最好制作一个jsfiddle。