控制器中的角度访问到嵌套的ng-repeat中的ng-model

时间:2017-01-05 16:05:45

标签: angularjs controller nested angularjs-ng-repeat angularjs-ng-model

我在主题中遇到问题。在ng-model中使用$ parent没有帮助。 我想要实现的是2个可点击按钮,将数量字段值增加和减少一个,并在网站底部显示摘要(每个价格*数量的总和)。

import array
import sys


ConstNoStudents = int(4)
Counter = int(0)
StudentMarkTest1 = array.array("d", range(ConstNoStudents + 1))
StudentMarkTest2 = array.array("d", range(ConstNoStudents + 1))
StudentName = []
StudentGender = []

while Counter <4:
  gender_value = int(0)
  Counter = Counter+1
  StudentName.append(input("Please Enter Student Name "))
  print(StudentName)
  while gender_value == 0:
    gender = input("Please Enter Student Gender ")
    if gender == 'm' or gender == 'f' or gender =='M' or gender == 'F':
      StudentGender.append(gender)
      gender_value = gender_value+1

    StudentMarkTest1[Counter] = int(input("Please Enter Mark for Test 1 "))
    StudentMarkTest2[Counter] = int(input("Please Enter Mark for Test 2 "))

print (StudentName)
print (StudentGender)
print (StudentMarkTest1)
print (StudentMarkTest2)
var app = angular.module("mainApp", []);
  app.controller("mainCtrl", function ($scope) {
  
  $scope.list1 = [
    { "uniqueId": "1", "name": "cat1" },
    { "uniqueId": "2", "name": "cat2" } 
  ];
  $scope.list2 = [
    { "uniqueId": "1", "name": "prod1", "price": "10" },
    { "uniqueId": "2", "name": "prod2", "price": "20" },
    { "uniqueId": "3", "name": "prod3", "price": "30" }
  ];
  
  // this one below doesn't work at all
  // $scope.quantity[1] = 1; 
  
  $scope.inc = inc;
    
  function inc(id) {
      console.log(id); //works fine
      // increase of exact quantity
  }
      
});

1 个答案:

答案 0 :(得分:0)

请记住,有多种方法可以解决这类问题,这里是我看到的大行以及每个步骤的示例:

  • 将您的数量存储在某处并初始化,例如在您的产品对象中,例如控制器中的示例:

    { "uniqueId": "1", "name": "prod1", "price": "10", quantity : 1 },

  • 创建一个增加的函数,另一个减少产品的数量,例如你的id:

    function increaseQuantity(id){ // Get your product in the list with a loop // If the product exists // Increase his quantity } function decreaseQuantity(id){ // Get your product in the list with a loop // If the product exists // Decrease his quantity } $scope.increaseQuantity = increaseQuantity; $scope.decreaseQuantity = decreaseQuantity;

  • 在您的按钮中调用正确的方法,例如视图中的示例:

    <td> quantity wanted: {{l2.quantity}} <button ng-click="increaseQuantity(l2.uniqueId)" type="button">+</button> <button ng-click="decreaseQuantity(l2.uniqueId)" type="button">-</button> </td>