具有索引范围变量数组的AngularJS动态模板

约翰·罗宾逊

我正在使用AngularJS,并尝试创建一个可以动态添加新输入的表单,类似于该小提琴:http : //jsfiddle.net/V4BqE/(下面的主要HTML,小提琴中的工作代码)。

<div ng-app="myApp" ng-controller="MyCtrl">
  <div add-input>
    <button>add input</button>
  </div>
</div>

我希望能够为表单使用HTML模板,因为我要添加的输入行长约为300行。我的问题是,在模板中使用时,我无法弄清楚如何索引包含数据的范围变量。我试图在plnkr http://plnkr.co/edit/4zeaFoDeX0sGTuBMCQP2?p=info上对上述代码进行修改但是,当我单击按钮时,没有表单元素出现。

在线(plnkr),我的template.html找不到404,但我认为这只是plnkr的限制。在使用Python HttpServer的计算机上,使用该方法时,我获得Error: [$parse:syntax]$templateRequestTypeError: Failed to execute 'appendChild' on 'Node': parameter 1 is not of type 'Node'.$http.get

有关使索引范围变量数组与外部html文件一起使用的任何建议?

谢谢,JR

编辑:更新plnkr链接

约瑟夫·哈鲁什(Jossef Harush)

您可以在没有指令和外部模板的情况下进行操作

您尝试执行的操作不需要指令(实际上,使用基本的angularjs工具非常简单)

http://plnkr.co/edit/LVzGN8D2WSL2nR1W7vJB?p=preview

html

<body>

  <div class="container" ng-app="myApp" ng-controller="MyCtrl">

    <button class="btn btn-primary" type="button" ng-click="addPhoneInput()">add input</button>

    <form>
      <div ng-repeat="item in telephoneNumbers">
        <hr>
        <input type="text" ng-model="item.phone">
      </div>
    </form>
    
    
    <hr>
    
    <div class="well">
      <h4>Phone Numbers,</h4>
      <p ng-repeat="item in telephoneNumbers">{{item.phone}}</p>
    </div>
  </div>
</body>

js

var app = angular.module('myApp', []);

app.controller('MyCtrl', ['$scope', function($scope) {
  // Define $scope.telephone as an array
  $scope.telephoneNumbers = [];

  $scope.addPhoneInput = function() {
    $scope.telephoneNumbers.push({});
  };

  // This is just so you can see the array values changing and working! Check your console as you're typing in the inputs :)
  $scope.$watch('telephoneNumbers', function(value) {
    console.log(value);
  }, true);
}]);

如果您坚持使用指令,

http://plnkr.co/edit/BGLqqTez2k9lUO0HZ5g1?p=preview

phone-number.template.html

<div>
  <hr>
  <input type="text" ng-model="ngModel" >
</div>

html

<body>

  <div class="container" ng-app="myApp" ng-controller="MyCtrl">

    <button class="btn btn-primary" type="button" ng-click="addPhoneInput()">add input</button>

    <form>
      <phone-number ng-repeat="item in telephoneNumbers" ng-model="item.phone"></phone-number>
    </form>
    
    
    <hr>
    
    <div class="well">
      <h4>Phone Numbers,</h4>
      <p ng-repeat="item in telephoneNumbers">{{item.phone}}</p>
    </div>
  </div>
</body>

js

var app = angular.module('myApp', []);

app.controller('MyCtrl', ['$scope', function($scope) {
  // Define $scope.telephone as an array
  $scope.telephoneNumbers = [];

  $scope.addPhoneInput = function() {
    $scope.telephoneNumbers.push({});
  };

  // This is just so you can see the array values changing and working! Check your console as you're typing in the inputs :)
  $scope.$watch('telephoneNumbers', function(value) {
    console.log(value);
  }, true);
}]);

app.directive('phoneNumber', function(){
  return {
    replace:true,
     scope: {
      ngModel: '=',
    },
    templateUrl: "phone-number.template.html"
  }
});

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

具有索引范围变量数组的AngularJS动态模板

来自分类Dev

SwiftUI具有动态数组的多个选择器,索引超出范围错误

来自分类Dev

具有动态索引的AWK数组

来自分类Dev

编译动态HTML并从JSON响应中绑定变量-具有隔离范围的指令-AngularJS

来自分类Dev

动态索引数组angularJs

来自分类Dev

动态模板和范围变量

来自分类Dev

模板中数组索引的范围

来自分类Dev

在angularjs中获取动态范围数组变量值

来自分类Dev

在angularjs中获取动态范围数组变量值

来自分类Dev

选择在numpy中具有可变索引范围的数组元素

来自分类Dev

具有相同路线的AngularJs动态模板显示

来自分类Dev

AngularJS:具有多个模板的动态自定义指令

来自分类Dev

C#LINQ创建具有动态索引的临时变量

来自分类Dev

C#LINQ创建具有动态索引的临时变量

来自分类Dev

angularjs 中的动态范围变量

来自分类Dev

具有动态范围的多维数组(无原始指针)

来自分类Dev

在PHP中动态创建具有索引值的数组

来自分类Dev

具有推论参数的模板函数指针数组变量

来自分类Dev

AngularJS选择具有ID和数组索引的选项

来自分类Dev

具有动态变量的uint8_t数组

来自分类Dev

AngularJS-具有动态大小的数组的ng模型

来自分类Dev

Ada动态数组分配索引范围

来自分类Dev

Angular 9如何删除具有用户指定索引范围的数组内的对象范围?

来自分类Dev

如何从给定的范围数组中获取具有最小最大值的匹配数组索引

来自分类Dev

返回范围变量的AngularJS动态Ng模型

来自分类Dev

获取动态范围变量值angularjs

来自分类Dev

访问变量时,“索引超出数组的范围”

来自分类Dev

如何合并具有相同数组名称的相同索引的数组动态值

来自分类Dev

用动态数组填充 angularjs 范围

Related 相关文章

  1. 1

    具有索引范围变量数组的AngularJS动态模板

  2. 2

    SwiftUI具有动态数组的多个选择器,索引超出范围错误

  3. 3

    具有动态索引的AWK数组

  4. 4

    编译动态HTML并从JSON响应中绑定变量-具有隔离范围的指令-AngularJS

  5. 5

    动态索引数组angularJs

  6. 6

    动态模板和范围变量

  7. 7

    模板中数组索引的范围

  8. 8

    在angularjs中获取动态范围数组变量值

  9. 9

    在angularjs中获取动态范围数组变量值

  10. 10

    选择在numpy中具有可变索引范围的数组元素

  11. 11

    具有相同路线的AngularJs动态模板显示

  12. 12

    AngularJS:具有多个模板的动态自定义指令

  13. 13

    C#LINQ创建具有动态索引的临时变量

  14. 14

    C#LINQ创建具有动态索引的临时变量

  15. 15

    angularjs 中的动态范围变量

  16. 16

    具有动态范围的多维数组(无原始指针)

  17. 17

    在PHP中动态创建具有索引值的数组

  18. 18

    具有推论参数的模板函数指针数组变量

  19. 19

    AngularJS选择具有ID和数组索引的选项

  20. 20

    具有动态变量的uint8_t数组

  21. 21

    AngularJS-具有动态大小的数组的ng模型

  22. 22

    Ada动态数组分配索引范围

  23. 23

    Angular 9如何删除具有用户指定索引范围的数组内的对象范围?

  24. 24

    如何从给定的范围数组中获取具有最小最大值的匹配数组索引

  25. 25

    返回范围变量的AngularJS动态Ng模型

  26. 26

    获取动态范围变量值angularjs

  27. 27

    访问变量时,“索引超出数组的范围”

  28. 28

    如何合并具有相同数组名称的相同索引的数组动态值

  29. 29

    用动态数组填充 angularjs 范围

热门标签

归档