angularjs指令 – 双向绑定Angularjs指令不工作
|
我一直在试图找出解决方案,但我认为我是一个死胡同.
所以这是我的指示 directives.directive('postprocess',function($compile)
{
return {
restrict : 'E',require: '^ngModel',scope: {
ngModel: '='
},link: function(scope,element,attrs) {
var parsed = scope.ngModel;
el = $compile(parsed)(scope);
element.html("");
//add some other html entities/styles.
element.append(el);
console.log(parsed);
}
};
});
的html <postprocess ng-model="some_model.its_property" style="padding-top: 10px;" /> 在控制器的某处,我更新模型属性 some_model.its_property = 'Holla'; 但是它不会更新相应的指令.它在加载时工作得很好,告诉我可能不是完全是一个范围问题.
这很简单,所以我已经删除了一些额外的代码.
请看下面的代码或工作Plunker: <!doctype html>
<html lang="en" ng-app="myApp">
<head>
<meta charset="UTF-8">
<title>Document</title>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.0.8/angular.min.js"></script>
<script>
var myApp = angular.module('myApp',[]);
myApp.directive('postprocess',function ($timeout) {
return {
restrict : 'E',transclude: 'true',scope: {
myVariable: '='
},attrs) {
$timeout(function () {
scope.myVariable = 'Bye bye!'
},200);
}
};
});
myApp.controller('myAppCtrl',['$scope','$timeout',function ($scope,$timeout) {
$scope.myVariable = {
value : 'Holla'
};
console.log($scope.myVariable.value); // -> prints initial value
$timeout(function () {
console.log($scope.myVariable.value); // -> prints value after it is changed by the directive
},2000);
}])
</script>
</head>
<body ng-controller="myAppCtrl">
<postprocess my-variable="myVariable.value" style="padding-top: 10px;" />
</body>
</html>
>控制器将初始值设置为“Holla” $watch和$apply
(编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
