如何从 attrs.$observe 绑定作用域?
<test func="hohoho"></test>
app.directive('test', function(){
return {
restrict: 'E'
, scope: { parentFunc: '@func'}
, link: function(scope, element, attrs) {
var func = '';
attrs.$observe('func', function(val) {
func = val;
console.log(func);
})
console.log('end');
console.log(func);
console.log(scope.parentFunc);
});
}
};
});
当我运行时,它会打印
end
undefined
undefined
(an empty string)
hohoho
为什么我在打印 func 和 parentFunc 时得到未定义?