Angularjs ngModel没有';t在使用ngInclude的自定义指令中更改原始变量
指令内的变量会更改,但指令外的变量不会更改。以下是实际代码: main.htmlAngularjs ngModel没有';t在使用ngInclude的自定义指令中更改原始变量,angularjs,angularjs-directive,angular-ngmodel,angularjs-ng-include,Angularjs,Angularjs Directive,Angular Ngmodel,Angularjs Ng Include,指令内的变量会更改,但指令外的变量不会更改。以下是实际代码: main.html <div class="row" ng-repeat="row in template"> <fx-widget type="{{row.type}}" value="post[row.name]"></fx-widget> {{post[row.name]}} </div> app.js app.directive('fxWidget', fun
<div class="row" ng-repeat="row in template">
<fx-widget type="{{row.type}}" value="post[row.name]"></fx-widget>
{{post[row.name]}}
</div>
app.js
app.directive('fxWidget', function() {
return {
restrict: 'E',
scope: {
value: '='
},
link: function($scope, $element, $attributes) {
$scope.typeUrl = '/partials/' + $attributes.type + '.html';
},
template: '<div ng-include="typeUrl"></div>',
};
});
partials/text.html
<input type="text" ng-model="value">
{{post[row.name]}
app.js
app.directive('fxWidget',function(){
返回{
限制:'E',
范围:{
值:'='
},
链接:函数($scope、$element、$attributes){
$scope.typeUrl='/partials/'+$attributes.type+'.html';
},
模板:“”,
};
});
partials/text.html
以下是我关于查找问题的测试和注释:
<input ng-model="post[row.name]"> {{post[row.name]}}
{{post[row.name]}
这个成功了template: '<input ng-model="value">
template: '<div ng-include="typeUrl"></div> {{value}}'
// ^^ doesn't work
模板:'
这一个确实有效,所以问题出在ng中template: '<input ng-model="value">
template: '<div ng-include="typeUrl"></div> {{value}}'
// ^^ doesn't work
模板:“{{value}”
//^^^不起作用
但是,在部分文件中,它可以工作:
partials/text.html
<input type="text" ng-model="value"> {{value}}
partials/text.html
{{value}}
ng include
会创建作用域的副本,这就是为什么它在父作用域中不会更改。如何使其更改范围
这也是一个离题的问题。我怎样才能摆脱ng include all和手动加载partials呢?templateUrl
参数可以接受带有属性的函数,但它不会编译绑定到实际变量的数据,因此这是不可能的
谢谢大家! 您必须编译模板,因为它是动态加载的。这篇文章应该能帮到你:谢谢!终于摆脱了ng,一切都解决了:D