简体   繁体   中英

Angularjs assigning dynamic ng-model name ,in constructing elements in list?

Here i defined my code, please help me in this. I am using angularjs in blade template engine of laravel, in this i couldn't dynamically assign name to ng-model in angular

 $scope.categoryChange=function(id,model){ $scope.getCategories(id,model); };//category change orgusers=0; $scope.users = new Array(); $scope.addUsers = function(){ if($scope.totalExperts>0){ orgusers++; newUser={"name":"name"+orgusers,"model":"expertise"+orgusers,"category":"empcategory"+orgusers}; $scope.users.push(newUser); if($scope.totalExperts!='' && $scope.totalExperts>0){ $scope.totalExperts--; } }else{ alert("To add users please input total experts field ."); document.getElementById('focusIt').focus(); } }; $scope.removeUser = function(removeUser){ $scope.users.splice($scope.users.indexOf(removeUser),1); if($scope.totalExperts!='' || $scope.totalExperts==0){ $scope.totalExperts++; } }; $scope.getCategories=function(catId,model){ experts=new Array(); angular.forEach($scope.categories, function(value,key) { if(catId==value.parent){ experts[key]={id:value.id,name:value.category}; } }); $scope[model]=experts; }; 
 <div> <ul style="list-style-type: none;"> <li ng-repeat="user in users"> <label>Name:</label><input type="text" name="<%user.name%>" /> <label style="display:inline-block;">Email:</label><input type="text" name="empemail[]" /> <label style="display:inline-block;">Phone:</label><input type="text" name="empphone[]" /> {{ Form::label('select category','',array('style'=>'display:inline-block;')) }} <select name="empcategories[]" ng-model="<%user.name%>" ng-options="k as v for (k,v) in mainCategories" ng-change="categoryChange(emp1,'<%user.model%>')" ></select> {{ Form::label('Expertise','',array('style'=>'display:inline-block;')) }} <select name="empspecialized[]" ng-model="empspecialization" ng-options="k as v.name for (k,v) in <% user.model %>"> </select> <button ng-click="removeUser(user)">-</button> </li> </ul> </div> 

Error: [ngModel:nonassign] Expression '<%user%>' is non-assignable. Element: any suggestions or solutions , am waiting thanks in advance

i found the solution to my posted question. The mistake i did, in this tried to assign value that is not assignable to the select option value as model value and i changed this to follwing code works for me.

<div>
<ul style="list-style-type: none;">
<li ng-repeat="user in users">
<label>Name:</label><input type="text" name="<%user.name%>" />
<label style="display:inline-block;">Email:</label><input type="text" name="empemail[]" />
<label style="display:inline-block;">Phone:</label><input type="text" name="empphone[]" />
{{ Form::label('select category','',array('style'=>'display:inline-block;')) }}
<select name="empcategories[]" ng-model="user.category" ng-options="k as v for (k,v) in mainCategories" ng-change="categoryChange(user.category,'<%user.model%>')" ></select>
{{ Form::label('Expertise','',array('style'=>'display:inline-block;')) }}
<select  name="empspecialized[]" ng-model="empspecialization" ng-options="k as v.name for (k,v) in <% user.model %>">
</select> 
<button ng-click="removeUser(user)">-</button>
</li>
</ul>
</div>

The result i required here is i need to pass the currently selected option id , to my calling method so that i can match and produce the sub-categories list. I hope it will help you , thank you

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM