I'm using angularjs and angular pagination to show users detail.
Here, I use ng-init
to initialize age of users. If there is age in user's detail then it will show otherwise it show default age 10.
Please see the Plunker
ng-init
works fine first time but when I move to next page it is not showing default age i.e.10.
For example: in Page 3, name is name30 and there is no age. So, age should come from default age.
ngInit
directive allows you to evaluate an expression in the current scope. The problem with your approach is that ngInit
is called only once when scope is created - so it works properly only for page 1.
Also check out what is says in AngularJs documentation:
The only appropriate use of ngInit is for aliasing special properties of ngRepeat, as seen in the demo below. Besides this case, you should use controllers rather than ngInit to initialize values on a scope.
So based on this recommendation it would be better to set default age value in the controller like:
// assign value on page change.
$scope.loadRecord = function(page) {
$scope.user = $scope.users[page-1];
if (!$scope.user.age) {
$scope.user.age = 10;
}
};
// initial value
$scope.loadRecord(1);
Please check out this Plunker to se it in action.