admin管理员组文章数量:1327926
Well, I have a issue here. I have an ng-repeat inside which an input with ng-change() is present. This is a part of directive template and double way binded to a parent object. If I type in something in input box, everything works fine and parent object is updated. However, when I have to replace the parent object from directive's controller, I face an issue.
The issue is that, once parent object gets replaced, the view is bound with new (replaced) values. Also at that time the same function (as in ng-change()) is triggered manually for some calculation.
However, I noticed that the same function is again getting called again (no idea how). The important thing is the ng-model of input is undefined, when they are automatically called. As a result, finally the parent object contains value undefined.
I am still confused, why the ng-change is getting called, after the controller method calls. Is it have to do something with child scopes that ng-repeat creates.
I have already used track by $index. and I have binded models to parentObj.something.something[$index]
Any help on above is appreciated...
I have
module.directive('myDirective', function () {
return {
scope: {
target: '=',
},
controller: 'DemoController',
templateUrl: 'app/demo/html/demo.html'
}
});
Main template:
<li ng-repeat="l in group_Main.mains"
<li ng-repeat="target in l.description.Value track by $index"
<li ng-repeat="(key, groups) in target.group track by $index">
<div layout="row" layout-wrap myDirective target="group"></div>
</li>
</li>
</li>
app/demo/html/demo.html::Directive's template
<div class="table_FY_height" flex ng-repeat="m in months track by $index">
<input ng-change="changeIt(target.targets.years[1].values.data[$index], target, year,parent, $index)"" ng-if="$index>currentMonth" ng-model="target.targets.years[1].values.data[$index]"/>
</div>
In directive's controller:
module.controller('DemoController', function($scope, $rootScope){
changeIt(-1,$scope.target,$scope.year,$scope.parent);
}
From directive's controller, I am trying to call a API and update target data as:
http.get(url).then({
function(APIResponse){
for(var i=0; i<12; i++){
target.targets.years[1].values.data[i] = APIResponse.targets.years[1].values.data[i]
}}, function(error){
//error handling here}
}
Doing this calls the dirrective and updates the view on screen with new values from APIResponse. Since, the directive view is controlled using ng-show, the new values remain intact in the view. This function is called once in controller per directive call with first argument as -1. But after that it again runs with first value as 'undefined'. With undefined, it runs as many times as the directive is piled. Consequently the target.targets.years[1].values.data[$index] bees undefined.
Any ideas whats going wrong? I have been scratching my head over it for hours.
Well, I have a issue here. I have an ng-repeat inside which an input with ng-change() is present. This is a part of directive template and double way binded to a parent object. If I type in something in input box, everything works fine and parent object is updated. However, when I have to replace the parent object from directive's controller, I face an issue.
The issue is that, once parent object gets replaced, the view is bound with new (replaced) values. Also at that time the same function (as in ng-change()) is triggered manually for some calculation.
However, I noticed that the same function is again getting called again (no idea how). The important thing is the ng-model of input is undefined, when they are automatically called. As a result, finally the parent object contains value undefined.
I am still confused, why the ng-change is getting called, after the controller method calls. Is it have to do something with child scopes that ng-repeat creates.
I have already used track by $index. and I have binded models to parentObj.something.something[$index]
Any help on above is appreciated...
I have
module.directive('myDirective', function () {
return {
scope: {
target: '=',
},
controller: 'DemoController',
templateUrl: 'app/demo/html/demo.html'
}
});
Main template:
<li ng-repeat="l in group_Main.mains"
<li ng-repeat="target in l.description.Value track by $index"
<li ng-repeat="(key, groups) in target.group track by $index">
<div layout="row" layout-wrap myDirective target="group"></div>
</li>
</li>
</li>
app/demo/html/demo.html::Directive's template
<div class="table_FY_height" flex ng-repeat="m in months track by $index">
<input ng-change="changeIt(target.targets.years[1].values.data[$index], target, year,parent, $index)"" ng-if="$index>currentMonth" ng-model="target.targets.years[1].values.data[$index]"/>
</div>
In directive's controller:
module.controller('DemoController', function($scope, $rootScope){
changeIt(-1,$scope.target,$scope.year,$scope.parent);
}
From directive's controller, I am trying to call a API and update target data as:
http.get(url).then({
function(APIResponse){
for(var i=0; i<12; i++){
target.targets.years[1].values.data[i] = APIResponse.targets.years[1].values.data[i]
}}, function(error){
//error handling here}
}
Doing this calls the dirrective and updates the view on screen with new values from APIResponse. Since, the directive view is controlled using ng-show, the new values remain intact in the view. This function is called once in controller per directive call with first argument as -1. But after that it again runs with first value as 'undefined'. With undefined, it runs as many times as the directive is piled. Consequently the target.targets.years[1].values.data[$index] bees undefined.
Any ideas whats going wrong? I have been scratching my head over it for hours.
Share Improve this question edited Nov 9, 2015 at 3:32 Saurabh Tiwari asked Nov 8, 2015 at 17:47 Saurabh TiwariSaurabh Tiwari 5,16111 gold badges50 silver badges90 bronze badges 8- could you add some code..? to get more idea about what is happening.. – Pankaj Parkar Commented Nov 8, 2015 at 17:52
- @PankajParkar Kindly have a look again. Bear with me in case, the language is not clear. – Saurabh Tiwari Commented Nov 9, 2015 at 3:34
- can you provide sample plunkr with sample data? – Grundy Commented Nov 9, 2015 at 3:54
- @SaurabhTiwari did the issue resolved for u ? any plunker or your data source would be great ! – ngCoder Commented Sep 24, 2016 at 10:14
- 1 If you've discovered the solution, please answer your own question and mark it as accepted – adamdport Commented Sep 26, 2016 at 15:00
1 Answer
Reset to default 8 +50I dug deep into the issue and found that in my case I had a directive
placed on input
tag, which was parsing the model after it is bound. (It was basically used for some kind of rounding off. So I removed that logic and started passing rounded figures from server. The issue ceased to appear). So my conclusion was that the second ng-change
is fired because the model is again changed by the directive. Anyone facing such issues should look for any other kind of change to the model after initial binding.
Posting this as the answer, as this was the solution in my case.
本文标签: javascriptngchange function called multiple times from input inside ngrepeatStack Overflow
版权声明:本文标题:javascript - ng-change function called multiple times from input inside ng-repeat - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742160171a2424799.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论