The scope variable SelectedPage is not updating.
There is a ul li for pagination, that has a dropdown & then a next button. User can go to a page either by clicking the button or choosing an option in select.
When I click on Next or select 2 in the dropdown, I'm expecting the value of $scope.SelectedPage to change to 2. But it always remains 1.
HTML:
<ul>
<li>
<select ng-model="SelectedPage" ng-change="ShowPageResult()">
<option value="1">1</option>
<option value="2">2</option>
</select>
</li>
<li ng-click="SelectedPage = SelectedPage + 1;ShowPageResult();"><a href=" #">Next</a></li>
</ul>
Ctrl.js
$scope.ShowPageResult = function () {
console.log($scope.SelectedPage); //always prints 1
}
EDIT http://jsfiddle.net/g8pLhf79/ After the page loads, click on 'Next', the no. increments. Now select a value in the dropdown and click on next. It now appends "1" instead of incrementing. I hope this helps understand the issue.
2 Answers 2
Here we go (2nd attempt): http://jsfiddle.net/g8pLhf79/11/
I made minimal changes to have the code work in your way, in general Angular is about separating View from Model/Controller so you're encouraged to keep the logic separated from the view. But anyway here it is the way you want it:
<div ng-app="app" ng-controller="ctrl">
<ul>
<li>
<select ng-model="SelectedPage" ng-change="ShowPageResult();">
<option value="1">1</option>
<option value="2">2</option>
</select>
</li>
<li ng-click="SelectedPage = (SelectedPage * 1+1);ShowPageResult();"><a href=" #">Next</a>
</li>
<li>Selected Page : {{SelectedPage}}</li>
</ul>
</div>
Controller:
var app = angular.module("app", []);
app.controller("ctrl", function ($scope) {
$scope.SelectedPage = 1;
$scope.ShowPageResult = function () {
console.log($scope.SelectedPage);
}
});
1 Comment
Here is the working example you can refer the plunker.
http://plnkr.co/edit/wOXWXvPZw9AYrqyy486h?p=preview
var app = angular.module('plunker', []);
app.controller('MainCtrl', function($scope) {
$scope.name = 'World';
$scope.arr = ["0","1"];
$scope.ShowPageResult = function () {
alert($scope.SelectedPage); //always prints 1
}
});
plz visit this one according to your requirement. http://plnkr.co/edit/wOXWXvPZw9AYrqyy486h?p=preview
6 Comments
ng-click="SelectedPage = SelectedPage + 1;