【问题标题】:How to make models dynamic in my template angularjs如何在我的模板 angularjs 中使模型动态化
【发布时间】:2015-07-10 05:41:33
【问题描述】:

我的指令中有一个带有文本框的模板,单击按钮(添加)时,我重复相同的指令 10 次,因此文本框将出现 10 次,但每个文本框的 ng-model 将保持相同,我需要动态化,以便在模板 ng-model 的每次重复时变得不同。 问题是我无法为文本框创建动态 ng-model 以区分输入的值,以便我可以在控制器中访问它。如何使文本框模型动态化。

App.directive("configDirectives", function($compile) {
      return {
        restrict: 'EA',
        link: function(scope, element, $attr) {
          console.log('Scope in directive : ' + scope);
          scope.add = function() {
            console.log("Inside directive value of satCount", satCount++);
            $newDirective = angular.element('<add-config></add-config>');
            element.append($newDirective);
            $compile($newDirective)(scope);
            console.log('Scope in directive : ' + scope);
          }
        }
      }).directive("addConfig", function() {
        return {
          restrict: 'AE',
          template: '<div>{{scope.satCount}}' +
            '<input type="text" ng-model="x"/>' +
            '</div>',
          link: function(scope, element, attribute) {
            scope.remove = function() {
              element.remove();
            }
          }
        });
      <!-- Controller -->
      (function() {
        var self = null;
        var ConfigRuleClass = Class.extend({
          init: function($scope, configService) {
            self = this;
            self.$scope = $scope;
          },
          save: function() {
            console.log("values from parent configuration---");
            console.log("config1---", self.lstConfigs.name);
            console.log("Dynamic Filed Data" + self.dynamicConfigs);

          }
        });
        App.controller("ConfigRuleCntrl", ['$scope', 'configService', ConfigRuleClass]);
      })();
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div id="xx" data-ng-controller="ConfigRuleCntrl as y">
  <input type="text" ng-model="y.x" />
  <button data-ng-click="add()">Add</button>
  <br>
  <button data-ng-click="y.save()">SAVE</button>
  <config-directives></config-directives>
</div>

【问题讨论】:

  • 您可以为您的问题创建 plnkr 吗?
  • 如何重复文本框?
  • 应该是我们在这里缺少的 ng-repeat 吗?
  • 点击按钮(添加)我正在添加文本框,即每次点击只需要重复一次我的模板。使用指令重复它,我编写了一个在点击时调用的添加方法添加的
  • 你能把指令的代码和add()函数的代码贴出来吗,因为它们似乎是这里的主要代码......

标签: angularjs


【解决方案1】:

尝试使用此代码在使用输入时创建动态模型

工作代码点击Here

<input type="text" ng-model="newObject[item.name]">

要保持以前的数据使用副本

 var original = Data.data;
 $scope.newObject =angular.copy(original);

【讨论】:

  • 我同意你的解决方案,但我必须在点击时重复我的模板,每次点击只重复一次,我还必须维护以前的数据。所以我不能使用 ng-repeat。非常感谢为您的回复
  • 你肯定可以使用ng-repeat;将数据放入数组中,每次要添加新行时,向数组中添加一个新元素。 Angular 将完成剩下的工作。
【解决方案2】:

您可以创建一个包含 10 个对象的数组并用作 ng-repeat 的源。
对象如下:

[
   { id:1 value:'' },
   { id:2 value:'' },
   ...
]

还有html:

<input type="text" ng-repeat="entry in entries track by entry.id" ng-model="entry.value"></input>

我创建了一个 jsFiddle 来展示它是如何工作的: http://jsfiddle.net/un1g3fao/2/

【讨论】:

  • 我不能使用 ng-repeat 因为点击时我只想重复我的模板一次,我需要不使用 ng-repeat 的解决方案。
  • 那么您只需将 1 个条目添加到重复源即可。或者,如果这不合适,请包含一个 plunker 以显示您的具体问题。
  • 如果要求是 100 次我不能在点击时使用 ng-repeat 只在我的模板应该重复时使用。非常感谢您的回复
  • @satyendrakumar 你在这里的评论没有意义。你是什​​么意思,你的模板应该只重复一次?你的代码肯定是在向你展示 trying 重复多次......
  • 我添加了一个 fiddel 来展示它是如何工作的,并检查它是否是你需要的。您能否描述一下小提琴中的行为与您的要求有何不同?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-11-25
  • 2013-01-29
  • 1970-01-01
  • 2013-06-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多