Angularjs get ng-repeat dynamically generated Ng-model value instance detailed _angularjs

Source: Internet
Author: User

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]:

    1. 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.
    2. $index means ng-repeat= "param in params" when traversing the subscript
    3. 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!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.