How to generates dynamically ng-model=“my_{{$index}}” with ng-repeat in AngularJS?

前端 未结 6 637
被撕碎了的回忆
被撕碎了的回忆 2020-11-29 08:16

I would like to ask you if you can give me a hand on this.

I have created a jsfiddle with my problem here. I need to generate dynamically some inputs with ng-model i

相关标签:
6条回答
  • 2020-11-29 08:25

    I need to generate dynamically some inputs with ng-model in a ng-repeater using the way ng-model="my_{{$index}}".

    One can use the this identifier with a bracket notation property accessor:

      <tr ng-repeat="param in parameters">
        <td>{{param}}:</td>
        <td>
          <input type="text" ng-model="this['my_'+$index]" />
          my_{{$index}}
        </td>
      </tr>
    

    It is possible to access the $scope object using the identifier this.

    For more information, see

    • AngularJS Developer Guide - Expression Context
    0 讨论(0)
  • 2020-11-29 08:31

    Beterraba's answer was very helpful for me. However, when I had to migrate the solution to Typescript it wouldn't behave for me. Here is what I did instead. I expanded the individual parameters (fields on the queryList in your example) into full objects that included a "value" field. I then bound to the "value" field and it worked great!

    Originally you had:

    [
      { name: 'Check Users', fields: [ "Name", "Id"] },
        ...
      }
    ]
    

    I changed it to something like this:

    [
      { name: 'Check Users', fields: [
                                        {Text:"Name",Value:""},
                                        {Text:"Id",Value:0}],
                                        ...
                                       ]
      }
    ]
    

    ...and bound to the 'Value' sub-field.

    Here is my Typescript if you care.

    In the html:

    <div ng-repeat="param in ctrl.sprocparams" >
        <sproc-param param="param" />
    </div>
    

    In the sproc-param directive that uses Angular Material. See where I bind the ng-model to param.Value:

    return {
        restrict: 'E',
        template: `
                    <md-input-container class="md-block" flex-gt-sm>
                        <label>{{param.Name}}</label>
                        <input  name="{{param.Name}}" ng-model=param.Value></input>
                    </md-input-container>`,
        scope: {
                param: "="
            }
    }
    
    0 讨论(0)
  • 2020-11-29 08:38

    Is there a reason to generate those field names? Can you treat each field as an object with name and value instead of a string name? (FIDDLE)

    function MainCtrl($scope) {
         $scope.queryList = [
             { name: 'Check Users', fields: [ { name: "Name" },  { name: "Id" } ] },
             { name: 'Audit Report', fields: [] },
             { name: 'Bounce Back Report', fields: [ { name: "Date" } ] } 
          ];
    }
    

    And just repeat off of selectedQuery.fields:

    <tr ng-repeat="field in selectedQuery.fields">
        <td>{{field.name}}:</td>
        <td><input type="text" ng-model="field.value" /></td>
    </tr>
    
    0 讨论(0)
  • 2020-11-29 08:40

    What you could do is to create an object on a scope (say, values) and bind to the properties of this object like so:

    <input type="text" ng-model="values['field_' + $index]" />
    

    Here is a jsFiddle illustrating the complete solution: http://jsfiddle.net/KjsWL/

    0 讨论(0)
  • 2020-11-29 08:42

    I did elaborate my answer from pkozlowski's and try to generate a dynamic form, with dynamic ng-model:

    <form ng-submit="testDynamic(human)">
        <input type="text" ng-model="human.adult[($index+1)].name">
        <input type="text" ng-model="human.adult[($index+1)].sex">
        <input type="text" ng-model="human.adult[($index+1)].age">
    </form>
    

    But first, we need to define the 'human' scope inside our controller

    $scope.human= {};
    

    And then, on submission we will have the data like this (depending on how much field is generated):

    var name = human.adult[i].name;
    var sex = human.adult[i].sex;
    var age = human.adult[i].age;
    

    It's pretty straightforward and I hope my answer helps.

    0 讨论(0)
  • 2020-11-29 08:46

    Does it solve your problem?

    function MainCtrl($scope) {
         $scope.queryList = [
            { name: 'Check Users', fields: [ "Name", "Id"] },
            { name: 'Audit Report', fields: [] },
            { name: 'Bounce Back Report', fields: [ "Date"] } 
          ];
        $scope.models = {};
        $scope.$watch('selectedQuery', function (newVal, oldVal) {
            if ($scope.selectedQuery) {
                $scope.parameters = $scope.selectedQuery.fields;
            }
        });
    }
    

    And in your controller:

      <tr ng-repeat="param in parameters">
        <td>{{param}}:</td>
        <td><input type="text" ng-model="models[param] " />field_{{$index}}</td>
      </tr>
    

    Fiddle

    0 讨论(0)
提交回复
热议问题