2016-05-11 68 views
0

在角度,我有一個對象:如何使用NG重複妥善

$scope.myobj = { 
"a":"", 
"b":"", 
"c":"" 
} 

我希望能夠在一個文本框中輸入使用NG重複與NG-模型,使得打字的文本框將更新「a」,「b」和「c」。現在我的HTML是:

<div ng-repeat="(keyname, keyvalue) in myobj"> 
    <input ng-model="keyvalue" value="{{keyvalue}}"> 
</div> 
+0

試試這個'<輸入NG-模型= 「鍵名」>' –

+0

@SSH它填充文本輸入...屬性的名稱,不起作用。 – Rolando

回答

3

你可以試試這個工作,

<div ng-repeat="(keyname, keyvalue) in myobj"> 
    <input ng-model="myobj[keyname]" > 
    </div> 

var app = angular.module("myApp", []); 
 
app.controller("myCtrl", function($scope) { 
 
$scope.myobj = { 
 
"a":"", 
 
"b":"", 
 
"c":"" 
 
} 
 
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script> 
 
<div ng-app="myApp" ng-controller="myCtrl"> 
 
<div ng-repeat="(keyname, keyvalue) in myobj"> 
 
    <input ng-model="myobj[keyname]" > 
 
</div> 
 
OBJECT VALUES : {{myobj}} 
 
</div>

0

您可能需要更改一下你的obj。讓它陣列(運行的代碼片段)

(function() 
 
{ 
 
    var app = angular.module("app", []); 
 

 
    function HomeController() 
 
    { 
 
     var vm = this; 
 
     vm.myobj = [{"name":"a"},{"name":"b"},{"name":"c"}]; 
 

 
    } 
 

 
    app.controller("HomeController", [HomeController]); 
 

 
})();
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script> 
 
<!DOCTYPE html> 
 
<html lang="en" ng-app="app"> 
 
    <head> 
 
     <meta charset="UTF-8"> 
 
     <title>Angular JS App</title>  
 
    </head> 
 
    <body> 
 

 
     <div class="container" ng-controller="HomeController as homeCtrl"> 
 

 
      <div class="row"> 
 

 
       <div ng-repeat="item in homeCtrl.myobj"> 
 
        <input ng-model="item.name" value="{{item.name}}"> 
 
       </div> 
 
       {{homeCtrl.myobj | json}} 
 

 
      </div><!--end of first row-->    
 
     </div> 
 
    </body> 
 
</html>

0

使用鍵名,而不是鍵值。 也給你的數組鍵值。 然後嘗試與keyvalue也。 將肯定

<!DOCTYPE html> 
<html> 
    <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script> 
    <body> 
     <div ng-app="myApp" ng-controller="myCtrl"> 
     <div ng-repeat="(keyname, keyvalue) in myobj"> 
      <input ng-model="keyname" value="{{keyvalue}}"> 
     </div> 
     </div> 
     <script> 
     var app = angular.module('myApp', []); 
     app.controller('myCtrl', function($scope) { 
      $scope.myobj = { 
     "a":"1", 
     "b":"2", 
     "c":"3" 
     } 
     }); 
     </script> 
    </body> 
</html>