更新Angular Directive的值时出现问题's模型,同时从表单的监视进行更新
Issue while updating value of Angular Directive's model while updating from watch of form
Am试图通过在表单模型的$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);
});
}]);
从本质上讲,当我的表单被加载并可通过控制器访问时,我需要更改指令中的一些数据。
您正在嵌套的不同DOM元素上实例化控制器两次。
<!-- Creates a new scope -->
<div ng-controller="ctrl">
<!-- Is pointing to the parent scope -->
<test-directive my-string="myString"></test-directive>
<!-- Creates a a new nested scope -->
<form name="myForm" ng-controller="ctrl" >
userType: <input name="input" ng-model="userType">
</form>
这意味着子作用域将获得它自己的属性myString
,该属性是在$watch
被激发时更新的属性。
但是,您的自定义指令指向父属性,因此不会反映更改。
相关文章:
- 视图ngValue-Angular JS中未更新模型的更改
- 需要使用触摸屏从 AngularJS 指令中更新模型
- 角度指令没有更新模型视图
- 复选框仅在第二次单击后更新模型
- Angular.js-Socket.io事件更新模型,而不是视图
- UI Select在删除后不会更新模型
- 为什么ng提交没有提交和更新模型
- AngularJS-基于URL更新模型
- 视图更改时,Ember.js更新模型
- Knockoutjs函数更新模型时应出现异常
- 挖空绑定不会更新模型
- 范围不更新模型中的更改
- 导航时更新模型变量
- 角度.js更新模型
- Ember.js不更新模型
- 在使用挖空自定义绑定时更新模型时更新元素
- 在自我更新时以ng重复更新模型
- 如何在主干中对列表进行排序时更新模型的顺序属性
- AngularJS 复选框不更新模型
- 更新模型中的数据不会绑定到控制器