如何声明动态 ng-model? angularjs
How to declare a dynamic ng-model? angularjs
我正在尝试进行动态输入。是否可以通过索引获得动态 ng-model?
See scrnsht here
应该将上面的条形码 ng-model 设置为 ng-model="barcode_0" 和 ng-model="barcode_1"
我试过这个代码,但它不起作用
//controller
console.log($scope['Barcode_' + index])
<!-- HTML -->
<input type="text" ng-model="Barcode_[$index]" class="form-control" placeholder="Stock ID" >
任何人?请帮忙
你走在正确的轨道上,你只需要对代码做一点改动。
Barcode_[$index]
是数组 Barcode_
的一个元素。您只需要在 controller
中 initialize
这个数组,然后您可以在 controller
中使用 $scope.Barcode_[0]
和 $scope.Barcode_[1]
来访问变量。
保持 html 代码不变,这是您的控制器:
$scope.Barcode_ = [];//initializing the array
您可以在这个控制器中像这样访问变量:
console.log($scope.Barcode_[0]);//prints first one
console.log($scope.Barcode_[1]);//prints second one
我正在尝试进行动态输入。是否可以通过索引获得动态 ng-model?
See scrnsht here
应该将上面的条形码 ng-model 设置为 ng-model="barcode_0" 和 ng-model="barcode_1"
我试过这个代码,但它不起作用
//controller
console.log($scope['Barcode_' + index])
<!-- HTML -->
<input type="text" ng-model="Barcode_[$index]" class="form-control" placeholder="Stock ID" >
你走在正确的轨道上,你只需要对代码做一点改动。
Barcode_[$index]
是数组 Barcode_
的一个元素。您只需要在 controller
中 initialize
这个数组,然后您可以在 controller
中使用 $scope.Barcode_[0]
和 $scope.Barcode_[1]
来访问变量。
保持 html 代码不变,这是您的控制器:
$scope.Barcode_ = [];//initializing the array
您可以在这个控制器中像这样访问变量:
console.log($scope.Barcode_[0]);//prints first one
console.log($scope.Barcode_[1]);//prints second one