如何在 Angular 中创建动态 ng-model
How to create Dynamic ng-model in Angular
如何在 Angular 中创建动态 ng-models?我试过了,但我觉得很难达到 运行。我得到的只是未定义的。这是我的代码。
<input type="text" class="form-control input-sm" ng model="teller[value.teller_id]" value="<% value.mac_address %>">
我打算拥有一个名称相同但仅按编号变化的 ng-model。我有一个包含 1-4 数字的对象。即 value.teller_id。我想将它作为 teller1、teller2、teller3(teller[value.teller_id] 等附加到 ng-model 中。它由 ng-repeat 循环,我没有任何问题,而是创建动态 ng-模型和更改数字将产生未定义。
ng model="teller[value.teller_id]" <---- doesn't work
我想实现 teller1, teller2 等..
谢谢!
是的。我们可以动态生成 ng-model 名称。
var app = angular.module('myApp', []);
app.controller('MainCtrl', function($scope) {
$scope.teller = {};
$scope.name = [{
"id":1
},{
"id":2
}]
});
<!DOCTYPE html>
<html ng-app="myApp">
<head>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.28/angular.min.js"></script>
<link rel="stylesheet" href="style.css">
<script src="script.js"></script>
</head>
<body ng-controller="MainCtrl">
<div ng-repeat="x in name">
<input type="text" ng-model="teller[x.id]" />
</div>
<pre>{{teller | json}}</pre>
</body>
</html>
希望对你有用:)
如何在 Angular 中创建动态 ng-models?我试过了,但我觉得很难达到 运行。我得到的只是未定义的。这是我的代码。
<input type="text" class="form-control input-sm" ng model="teller[value.teller_id]" value="<% value.mac_address %>">
我打算拥有一个名称相同但仅按编号变化的 ng-model。我有一个包含 1-4 数字的对象。即 value.teller_id。我想将它作为 teller1、teller2、teller3(teller[value.teller_id] 等附加到 ng-model 中。它由 ng-repeat 循环,我没有任何问题,而是创建动态 ng-模型和更改数字将产生未定义。
ng model="teller[value.teller_id]" <---- doesn't work
我想实现 teller1, teller2 等..
谢谢!
是的。我们可以动态生成 ng-model 名称。
var app = angular.module('myApp', []);
app.controller('MainCtrl', function($scope) {
$scope.teller = {};
$scope.name = [{
"id":1
},{
"id":2
}]
});
<!DOCTYPE html>
<html ng-app="myApp">
<head>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.28/angular.min.js"></script>
<link rel="stylesheet" href="style.css">
<script src="script.js"></script>
</head>
<body ng-controller="MainCtrl">
<div ng-repeat="x in name">
<input type="text" ng-model="teller[x.id]" />
</div>
<pre>{{teller | json}}</pre>
</body>
</html>
希望对你有用:)