angular.js, can't edit dynamically created input fields

后端 未结 3 2091
梦如初夏
梦如初夏 2021-01-02 00:58

Using angular.js, I have a dynamic list of form fields I want to display to the user for editing (and later submission):

var app = angular.module(\'app\', []         


        
相关标签:
3条回答
  • 2021-01-02 01:18

    You need to use an array of objects. Hopefully you can rework your model:

    $scope.fields = [
       {label: "foo", value: "foov"},
       {label: "bar", value: "barv"},
       {label: "baz", value: "bazv"}
    ];
    
    <tr ng-repeat="field in fields">
      <td>{{field.label}}:</td>
      <td><input type="text" ng-model="field.value">
    

    Fiddle.

    0 讨论(0)
  • 2021-01-02 01:19

    SET answered why it's happening, but a work-around to achieve the desired behavior would be to maintain a separate array of your keys, and run ng-repeat off those keys. I added some text fields for testing to add more properties to $scope.fields

    You could use $watch to dynamically set the keys when the property count changes, if your requirements were that the field count may change.

    http://jsfiddle.net/aERwc/10/

    markup

    <div ng-app="app" ng-controller="Ctrl">
        <table>
            <th>key</th>
            <th>value</th>
            <tr ng-repeat="key in fieldKeys">
                <td>{{key}}:</td>
                <td><input type="text" ng-model="fields[key]"/></td>
            </tr>
        </table>
        <div><h6>Add a field</h6>
            key: <input type="text" ng-model="keyToAdd" /><br />
            value: <input type="text" ng-model="valueToAdd" />
            <button ng-click="addField()">Add Field</button>
        </div>
    </div>
    

    controller

    var app = angular.module('app', []);
    
    app.controller('Ctrl', function($scope) {
        $scope.fields = {
            foo: "foo",
            bar: "bar",
            baz: "baz"
        };
        $scope.fieldKeys = [];
    
        $scope.setFieldKeys = function() {
            var keys = [];
            for (key in $scope.fields) {
                keys.push(key);
            }
            $scope.fieldKeys = keys;
        }
    
        $scope.addField = function() {
            $scope.fields[$scope.keyToAdd] = $scope.valueToAdd;
            $scope.setFieldKeys();
            $scope.keyToAdd = '';
            $scope.valueToAdd = '';
        }
    
        $scope.setFieldKeys();
    });
    
    0 讨论(0)
  • 2021-01-02 01:31

    It is editable but after each key press your text field losing focus so that you have to click on it again to put another char.

    And that happens because whole you template being re-rendered after each change in any of models. And after template re-rendered, currently there is no way to know which input should be focused. So you should create that way and you may want to write directive to hold focus on selected input.

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