【发布时间】:2015-11-24 17:11:22
【问题描述】:
我正在尝试通过在表单模型的 $watch 期间更新的模型来更新角度指令的绑定。尽管模型的值在表单监视的控制器范围内更新,但它不会在指令渲染中更新。有什么想法我可能会出错吗?
<html ng-app="App">
<head>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/angularjs/1.2.1/angular.min.js"></script>
<script type="text/javascript" src="script.js"></script>
</head>
<body>
<div ng-controller="ctrl">
<test-directive my-string="myString"></test-directive>
<form name="myForm" ng-controller="ctrl" >
userType: <input name="input" ng-model="userType">
</form>
<input type="text" name="input" ng-model="myModel" required />
{{testString}}
</div>
</body>
</html>
这是 Angular 设置。
var app = angular.module('App', []);
app.directive('testDirective', function ($parse) {
return{
restrict: 'E',
scope:{myString:"=myString"},
link: function(scope,element,attributes){
var test=[];
},
template:' Updated::{{myString}}',
}
});
app.controller('ctrl',['$scope','$timeout',function($scope,$timeout) {
$scope.myString='12345';
$scope.$watch('myForm', function(myForm,oldvalue) {
debugger;
if(myForm) {
$scope.myString='new String';
}
else {
console.log('Form is Undefined');
}
});
$scope.$watch('myModel',function(newValue,oldValue){
console.log(newValue);
});
}]);
基本上,当我的表单被加载并通过控制器访问时,我需要更改指令中的一些数据。
【问题讨论】:
-
请不要包含您的代码图片,只需包含您的代码即可。 Stack Overflow 有非常好的代码格式。在此处包含相关详细信息会使您的问题更容易回答,并且对遇到相同问题的人更有帮助。
-
嘿@SunilD.,已更新格式。
标签: javascript angularjs angularjs-directive