2017-07-07 35 views
0

當我嘗試輸入隨機數據爲通過.push方法它不是在這裏工作 數組或JSON文件.push方法是我的代碼 陣列碼是角JS的JSON文件無法正常工作

$scope.stock =[{ 
"name":"Pepsi", 
"price":65, 
"color":"black", 
"avaliable":true}] 

嘗試通過NG-模型結合

<form ng-submit="addItem()"> 
    <input type="text" placeholder="Name" ng-model="newName" /> 
    <input type="text" placeholder="Price" ng-model="newPrice" /> 
    <input type="text" placeholder="color" ng-model="newcolor" /> 
    <input class="submit" type="submit" value="Add New Item" /> 
</form> 

JS代碼是

$scope.addItem = function(){ 
    $scope.stock.push({ 
     name: $scope.newName, 
     price: parseInt($scope.newPrice), 
     color: $scope.newcolor, 
     avaliable: true 
    }); 
+0

你得到任何錯誤?我認爲你的代碼看起來不錯 –

+0

你初始化了$ scope.stock = []; – Vivz

+0

沒有給我任何東西? –

回答

0
//try this 
$scope.stock = []; 

$scope.addItem = function(){ 
$scope.stock.push({ 
    name: $scope.newName, 
    price: parseInt($scope.newPrice), 
    color: $scope.newcolor, 
    avaliable: true 
}); 
0

其工作的角度1.4.6,請點擊這裏查看Jsfiddle demo

angular.module('myApp', ['ngStorage']).controller('ctrl', 
    function($scope, $localStorage) { 

    $scope.stock = [{ 
     "name": "Pepsi", 
     "price": 65, 
     "color": "black", 
     "avaliable": true 
    }] 

    $scope.addItem = function() { 
     $scope.stock.push({ 
     name: $scope.newName, 
     price: parseInt($scope.newPrice), 
     color: $scope.newcolor, 
     avaliable: true 
     }); 
    } 
    });