Angularjs gets the ng-repeat dynamically generated Ng-model value
The recent project encountered Ng-model is ng-repeat dynamically generated, ng-model= "variable", what variable, is unknown, so you cannot be in $scope. " Variable "to the value, even if the value is one of the values, such a problem, through Baidu find a solution, here record, also can help everyone.
Code
Html
<div>
<div class= "Modal-header" >
Js
var Modalinstancectrl = function ($scope, $modalInstance, params) {
$scope. params = params;
$scope. conf = [];
$scope. OK = function () {
console.log ($scope. conf);
$modalInstance. Close ($scope. conf);
$scope. Cancel = function () {
$modalInstance. Dismiss (' Cancel ');
Problem description
Because Ng-model is ng-repeat dynamically generated, ng-model= "variables" and what variables are unknown, so you can't $scope. " Variable "to the value, even if the value is one of the values, this problem bothers me a day, finally resolved."
Solving method
First Ng-model set to $parent.conf[$index]:
- The reason for using $parent is ng-repeat, which generates a child scope object for each input, and $parent represents the scope of the parent class, so that we can get the value in the JS file.
- $index means ng-repeat= "param in params" when traversing the subscript
- conf is the actual effect of our variable name in JS
We have defined a $scope.conf = [] in controller, an array that just passes through the code above, adds elements to the array, and then we automatically save all of Ng-model's elements by scope.conf.
Actual effect:
Thank you for reading, I hope to help you, thank you for your support for this site!