Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I set a dynamic model name in AngularJS?

Tags:

angularjs

I want to populate a form with some dynamic questions (fiddle here):

<div ng-app ng-controller="QuestionController">     <ul ng-repeat="question in Questions">         <li>             <div>{{question.Text}}</div>             <select ng-model="Answers['{{question.Name}}']" ng-options="option for option in question.Options">             </select>         </li>     </ul>      <a ng-click="ShowAnswers()">Submit</a> </div> ​ function QuestionController($scope) {     $scope.Answers = {};      $scope.Questions = [     {         "Text": "Gender?",         "Name": "GenderQuestion",         "Options": ["Male", "Female"]},     {         "Text": "Favorite color?",         "Name": "ColorQuestion",         "Options": ["Red", "Blue", "Green"]}     ];      $scope.ShowAnswers = function()     {         alert($scope.Answers["GenderQuestion"]);         alert($scope.Answers["{{question.Name}}"]);     }; }​ 

Everything works, except the model is literally Answers["{{question.Name}}"], instead of the evaluated Answers["GenderQuestion"]. How can I set that model name dynamically?

like image 915
Mike Pateras Avatar asked Sep 23 '12 15:09

Mike Pateras


People also ask

Which method is used to create models in AngularJS?

In basic examples, AngularJS uses the $scope object as the model. However, in the previous article I showed how using the controllerAs method allowed the controller itself to be added to the scope with a given name and essentially be used as the model.

What is data ng model in AngularJS?

ngModel is a directive which binds input, select and textarea, and stores the required user value in a variable and we can use that variable whenever we require that value. It also is used during validations in a form.

Can we use NG model for Div?

NgModel expects the bound element to have a value property, which div s don't have. That's why you get the No value accessor error. I don't know if the input event is supported on all browsers for contenteditable . You could always bind to some keyboard event instead.


2 Answers

http://jsfiddle.net/DrQ77/

You can simply put javascript expression in ng-model.

like image 135
Tosh Avatar answered Oct 07 '22 19:10

Tosh


You can use something like this scopeValue[field], but if your field is in another object you will need another solution.

To solve all kind of situations, you can use this directive:

this.app.directive('dynamicModel', ['$compile', '$parse', function ($compile, $parse) {     return {         restrict: 'A',         terminal: true,         priority: 100000,         link: function (scope, elem) {             var name = $parse(elem.attr('dynamic-model'))(scope);             elem.removeAttr('dynamic-model');             elem.attr('ng-model', name);             $compile(elem)(scope);         }     }; }]); 

Html example:

<input dynamic-model="'scopeValue.' + field" type="text"> 
like image 37
William Weckl Avatar answered Oct 07 '22 21:10

William Weckl