javascript - Angularjs need ng-model update before ng-change -


i create customdirective , want ng-model update before ng-change fire. ng-change fire before udpate ng-mdoel value below code.

main issue coming when change page number in dropdown list. alert previous value. think ng-mdoel update after ng-change fire. want ng-model fire before ng-change.

app.directive('bottompagination', function () {     var directive = {         templateurl: '/app/common/directives/bottompagination.html',         restrict: 'a',         replace: true,         scope: {             currentpage: '=',             changepagesize: '&'         }     };     return directive; });  //here html directive (/app/common/directives/bottompagination.html)         <select  id="pagesizeddl" ng-model="pagesize" ng-change="changepagesize()">                             <option>5</option>                             <option>10</option>                             <option>20</option>                             <option>30</option>                         </select>  // here html page used directive   <div data-ng-controller="productslist vm">                 <div data-bottom-pagination                         data-page-size="vm.paging.pagesize"                         data-change-page-size="vm.changepagesize()"                          >                     </div>   </div>  // contrller (function () {     // start products list function     var listcontrollerid = 'productslist';     angular.module('app').controller(listcontrollerid,         ['$scope', listcontroller]);      function listcontroller($scope) {         var vm = this;          vm.paging = {             pagesize: 10         };           vm.changepagesize = changepagesize;          function changepagesize() {             alert(vm.paging.pagesize);         }     } })(); 

hi had same problem.

my best practice solution add parameter in ng-change function in directive template. need add parameter json object!

<select  id="pagesizeddl" ng-model="pagesize" ng-change="changepagesize({pagesize:pagesize})">                         <option>5</option>                         <option>10</option>                         <option>20</option>                         <option>30</option>                     </select> 

the property "pagesize" (of json object) matched parameter of directive function vm.changepagesize(pagesize). necessary both have same name!

<div data-ng-controller="productslist vm">             <div data-bottom-pagination                     data-page-size="vm.paging.pagesize"                     data-change-page-size="vm.changepagesize(pagesize)"                      >                 </div> 

have alter controller function this:

function changepagesize(pagesize) {         alert(pagesize);     } 

Comments

Popular posts from this blog

java - nested exception is org.hibernate.exception.SQLGrammarException: could not extract ResultSet Hibernate+SpringMVC -

sql - Postgresql tables exists, but getting "relation does not exist" when querying -

asp.net mvc - breakpoint on javascript in CSHTML? -