简体   繁体   English

使用JavaScript添加新的AngularJS变量

[英]Add new AngularJS variables with JavaScript

I'm trying to make a dynamic form with AngularJS and JavaScript. 我正在尝试使用AngularJS和JavaScript创建动态表单。 The objective is to add how many inputs the user need and transform all those inputs in AngularJS variables that print it on body. 目的是增加用户需要的输入数量,并转换为将其打印在正文上的AngularJS变量中的所有这些输入。 So I got that code: 所以我得到了代码:

$(function(){
   var number = 1;
   $('a.add').click(function(e){
      e.preventDefault();
      $('#this_div_contains_settings').append('<input type="text" name="example'+number+'" ng-model="example'+number+'" placeholder="Anything">');
      number++;
   });
   $('#this_div_contains_settings').on('click','a.design_button', function(e){
      e.preventDefault();
      $(this).parent().remove();
   });
});

This function add a INPUT on my DIV with the different ng-model every time it run. 每次运行时,此函数都会在具有不同ng模型的DIV上添加一个INPUT。

The problem is, it just work's if the {{example1}} is already on my BODY, if I add it later with another function, it just doesn't work. 问题是,如果{{example1}}已经在我的BODY上,它就可以工作,如果以后再添加另一个功能,那将不起作用。

I'm new with AngularJS, so I didn't understand if I need to "refresh" the AngularJS everytime I add new variable or something like that. 我是AngularJS的新手,所以我不明白是否每次添加新变量或类似的东西时都需要“刷新” AngularJS。

Any help will be appreciated :) 任何帮助将不胜感激 :)

Using jQuery is the wrong way to solve this problem. 使用jQuery是解决此问题的错误方法。 Instead, create an array inside of your controller that will hold the models for all of these inputs. 而是在控制器内部创建一个数组,其中将包含所有这些输入的模型。

Depending on how you define/create your controllers, $scope may be replaced with this 这取决于你如何定义/创建你自己的控制器, $scope可以被替换为this

$scope.examples = [];

Next, create a function that will add a new example to the array: 接下来,创建一个将向数组添加新示例的函数:

$scope.addExample = function () {
    $scope.examples.push("");
}

Now, in your template, create the inputs using an ng-repeat: 现在,在您的模板中,使用ng-repeat创建输入:

<div id="this_div_contains_settings">
    <input ng-repeat="example in examples" type="text" name="example" ng-model="example" placeholder="Anything">
</div>

and have your "add" button call your addExample function on click: 并让您的“添加”按钮在点击时调用addExample函数:

<a class="add" ng-click="addExample()">Add Example</a>

Finally, remove all of the code that was included in your question. 最后,删除问题中包含的所有代码。

And for your .design_button that removes all the examples, that's easy too: 对于删除所有示例的.design_button ,这也很容易:

<a class="design_button" ng-click="examples = []">remove all examples!</a>

by that same concept, you could even remove the need for the addExample function, however i tend to keep logic in the controller (well, actually in the services, but that's another topic) anyway rather than putting it in the template. 通过相同的概念,您甚至可以消除对addExample函数的需要,但是无论如何,我倾向于将逻辑保留在控制器中(实际上,保留在服务中,但这是另一个主题),而不是将其放入模板中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM