如果ng-model没有改变,就无法使用ng-click将项目推送到JavaScript数组。

31 浏览
0 Comments

如果ng-model没有改变,就无法使用ng-click将项目推送到JavaScript数组。

这个问题已经有答案了

Angular ng-repeat 错误\"重复项在repeater中不被允许\"

我可以通过ng-click事件处理动态向无序列表添加项目(通过html输入)。每当我更改输入文本框的值时,就会发生这种情况。但是,如果我在不更新文本框的情况下单击添加按钮,则输入文本框的值不会被添加到列表中。

    
  • {{x}}

 

Write in the input field to add items.

 


admin 更改状态以发布 2023年5月21日
0
0 Comments

你需要按照以下方式添加track by $index。在ng-repeat指令中,AngularJS不允许重复。如果您尝试这样做,您将会得到错误。

如果您想允许重复,您需要按照以下方式更改代码。

   
  • {{x}}
  • 示例

    var app = angular.module("myShoppingList", []);
    app.controller("myCtrl", function($scope) {
      $scope.products = ["Milk", "Bread", "Cheese"];
      $scope.addItem = function() {
        $scope.products.push($scope.addMe);
      }
    });


    
    
      
      
    
    
          
    • {{x}}

    Write in the input field to add items.

    0