What is the angularjs way to databind many inputs?

后端 未结 5 1051
攒了一身酷
攒了一身酷 2020-11-28 22:07

I\'m learning angularjs and I want to be able let the user enter many inputs. When these inputs are entered the list array elements should change accordingly. I

5条回答
  •  挽巷
    挽巷 (楼主)
    2020-11-28 22:35

    Here's a way to do it. I used textareas and a different structure to my repeaters, but the main concept is:

    • display a simple value based on index. (not bound)
    • on blur update the model
    • on model update re-render

    It is essentially, faking the binding.

    Working fiddle - http://jsfiddle.net/VvnWY/4/

    The html:

      
    
    
    Here's a few strings:
    {{strings[$index]}}
    Here's the strings as editable (twice so that you can see the updates from a model change):

    The JS:

    var myApp = angular.module('myApp',[]);
    angular.module('myApp', [])
      .controller('MyCtrl', ['$scope', function($scope) {
        $scope.strings = [ "foo", "bar", "cow" ];
       }])
      .directive('formTextareas', function() {
        return {
            restrict: "E",
            scope: {
                strings: '='
            },
            templateUrl: "textareas.html",
            link: function( $scope ){
                $scope.blur = function( $event, $index ){
                    $scope.strings[ $index ] = $event.currentTarget.value;
                };
            }
        };
      })
    ;
    

提交回复
热议问题