在Angularjs控制器中使用来自jquery函数的临时变量

时间:2015-10-07 11:23:46

标签: jquery angularjs variables controller

我需要通过单击按钮添加到“订单列表”元素,而不是通过输入表单的用户输入。 如何在控制器中使用从$ click函数派生的var?

CODE

var app = angular.module('myApp', []);
app.controller('POSController', function ($scope) {
  var x=choose;

  $scope.food = {
    pizza       : {count: 1, id:2, detail: "Brick Oven Pizza", price: 15},
    donut       : {count: 3, id:3, detail: "Glazed Donut",price: 8},
    tortilla    : {count: 1, id:4, detail: "Tortilla Chips",price: 3},
    burger      : {count: 1, id:5, detail: "Burger",price: 3},
    samosa      : {count: 1, id:6, detail: "Delicious Samosas",price: 3},
    coldcoffee  : {count: 1, id:7, detail: "Cold Coffee",price: 2},
    hotcoffee   : {count: 1, id:8, detail: "Hot Coffee",price: 2},
    coke        : {count: 1, id:9, detail: "Coke",price: 2},
    dietcoke    : {count: 1, id:10, detail: "Diet Coke",price: 2},
    pepsi       : {count: 1, id:11, detail: "Pepsi",price: 2},
    incognita: {count: 1, id:12, detail: x, price: 2},
  };

});

我需要为数组的最后一个元素的变量x分配临时值,以便每个输入都有新值。 我附上了我的PLUNK http://plnkr.co/edit/CoSAgGAco016pEIcRtLi?p=preview

1 个答案:

答案 0 :(得分:0)

我添加了一个带有ng-click事件的按钮,如下所示:

<button ng-click='addthisItem(nome)'>Add item</button>

然后在你的控制器中:

$scope.addthisItem = function(nome) {

  if ($scope.food.hasOwnProperty(nome)) {
    $scope.add($scope.food[nome]);
  }
  else {
    //some error handling here
  }
}

这是带有代码的plunker

希望它有所帮助。