javascript - AngularJs - facing an issue when using ng-init -


i facing issue using ng-init , assign model inside html.

the following code works fine. following code add/edit functionality. example, when row is opened in edit mode persist existing value , shows in textbox.

<div>     <div ng-if="title == 'add student'">     <input type="text" name="name"placeholder="student name" data-ng-model="registration.student.firstname" maxlength="50">     </div>      <div ng-if="title == 'edit student'">     <input type="text" name="name"placeholder="student name" data-ng-model="student.student.firstname" maxlength="50">     </div> </div> 

however, following code short version of above code not work. mean when row opened in edit mode shows text field not show existing value (first name) in it. why?

<div ng-init="model = (title == 'add student' ? registration.student : student.student)">     <input type="text" name="name" placeholder="student name" data-ng-model="model.firstname" maxlength="50"> </div> 

please suggest whether ng-init can't used in way or issue in code?

thanks

controller

var currentstate = $state.current.name;     if if (currentstate == "add")     {         $scope.registration = {         student: {         firstname: '',     }         var _init = function () {     }         $scope.title = " add student";     }     else     {         $scope.student= {};         $scope.student= response[0];         var _init = function () {         $scope.title = " edit student";     } } 

you ng-init block wrong returning true or false, messing brackets.

markup

<div ng-init="model = (title == 'add student') ? registration.student : student.student">     <input type="text" name="name" placeholder="student name" data-ng-model="model.firstname" maxlength="50"> </div> 

update

in current case ng-init getting executed while element rendered on dom, @ instance on time registration.student & student.student doesn't have value. evaluation of ng-init setting null object model student. i'd suggest set model value controller logic more safer.

code

var currentstate = $state.current.name;     if (currentstate == "add")     {         $scope.registration = {         student: {         firstname: '',     }         var _init = function () {     }         $scope.title = " add student";     }     else     {         $scope.student= {};         $scope.student= response[0];         var _init = function () {         $scope.title = " edit student";     }     //shifted logic in controller     $scope.model = (title == 'add student' ? registration.student : student.student);  } 

markup

<div>     <input type="text" name="name" placeholder="student name"       data-ng-model="model.firstname" maxlength="50"/> </div> 

other way add 1 more flag loadeddata says ajax response has been fetched & registration.student & student.student values available in scope.

markup

<div ng-if="loadeddata">     <input type="text" name="name" placeholder="student name" data-ng-model="model.firstname" maxlength="50"> </div> 

code

var currentstate = $state.current.name;     if (currentstate == "add")     {         $scope.registration = {         student: {         firstname: '',     }         var _init = function () {     }         $scope.title = " add student";     }     else     {         $scope.student= {};         $scope.student= response[0];         var _init = function () {         $scope.title = " edit student";     }     //set flag     $scope.loadeddata = true;  } 

Comments

Popular posts from this blog

angularjs - ADAL JS Angular- WebAPI add a new role claim to the token -

php - CakePHP HttpSockets send array of paramms -

node.js - Using Node without global install -