2016-10-02 21 views
0
<!DOCTYPE html> 
<html> 
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script> 
<body> 

<script> 
var app = angular.module("myShoppingList", []); 
app.controller("myCtrl", function($scope) { 
    $scope.products = ["Milk", "Bread", "Cheese"]; 
    $scope.addItem = function() { 
     $scope.errortext = ""; 
     if (!$scope.addMe) {return;} 
     if ($scope.products.indexOf($scope.addMe) == -1) { 
      $scope.products.push($scope.addMe); 
     } else { 
      $scope.errortext = "The item is already in your shopping list."; 
     } 
    } 
    $scope.removeItem = function (x) { 
     $scope.errortext = ""; 
     $scope.products.splice(x, 1); 
    } 
}); 
</script> 

<div ng-app="myShoppingList" ng-controller="myCtrl"> 
    <ul> 
    <li ng-repeat="x in products">{{x}}<span ng-click="removeItem($index)">×</span></li> 
    </ul> 
    <input ng-model="addMe"> 
    <button ng-click="addItem()">Add</button> 
    <p>{{errortext}}</p> 
</div> 

</body> 
</html> 

<input ng-model="addMe">獲取輸入值,並添加到列表中
如果我想定義的<div>代替<input>送價值是什麼到我的控制器而不是<input>?我一直在嘗試這個很長的時間,並且無法獲得發送到控制器的包含在<div></div>之間的值。怎樣一個div值發送到功能的角度控制器

+0

你能更清楚一點嗎?你想發送給控制器什麼? –

+0

或更籠統地說;你想做什麼? – charlietfl

+0

的事情是,我有以下的div:

\t \t \t \t \t \t \t \t \t \t \t \t \t \t \t \t \t \t \t \t \t \t \t \t \t \t \t \t \t \t \t \t \t \t
{{ $index + 1 }}
\t \t \t \t \t \t \t \t \t \t \t \t \t \t \t \t \t
{{ x.product }}
\t \t \t \t \t \t \t \t \t \t \t \t \t \t \t \t \t
${{ x.price }}
\t \t \t \t \t \t \t \t \t \t \t \t \t \t \t \t \t
Add to cart
\t \t \t \t \t \t \t \t \t \t \t \t \t \t \t \t \t
我想要的是當我點擊添加到購物車按鈕,我應該看到它的車 – jack

回答

0

只需將您的x作爲參數addToCart將其添加到控制器中的購物車。

參見演示here

相關問題