2017-04-24 63 views
1

我無法理解如何將對象推入數組我嘗試了幾種不同的方法,仍然無法弄清楚。AngularJs - 在數組中推對象

var app = angular.module('myApp',[]) 
 
app.controller('dropdown', function($scope, $http){ 
 
\t $scope.userInfo = []; 
 
\t 
 
\t $scope.pushInArray = function() { 
 
\t \t $scope.userInfo.push($scope.users) 
 
\t } 
 

 
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script> 
 
<div ng-app="myApp" ng-controller="dropdown"> 
 

 
<input type="text" name="name" ng-model="users.name" placeholder="Name"> 
 
<input type="text" name="email" ng-model="users.email" placeholder="Email"> 
 
<input type="text" name="phoneNo" ng-model="users.phoneNo" placeholder="phone Number"> 
 
<button ng-click="pushInArray()">Add</button> 
 
<pre>{{userInfo}}</pre> 
 
</div>

在添加按鈕的點擊我推USERINFO電學性能的研究用戶的信息。我第一次工作,但如果我修改了已經存儲的值也被修改的值(在推動值正在修改之後)。

回答

1

嘗試angular.copy,這將是l用新實例複製存在的對象。

var app = angular.module('myApp',[]) 
 
app.controller('dropdown', function($scope, $http){ 
 
\t $scope.userInfo = []; 
 
\t 
 
\t $scope.pushInArray = function() { 
 
    var user = angular.copy($scope.users); 
 
\t \t $scope.userInfo.push(user); 
 
\t } 
 

 
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script> 
 
<div ng-app="myApp" ng-controller="dropdown"> 
 

 
<input type="text" name="name" ng-model="users.name" placeholder="Name"> 
 
<input type="text" name="email" ng-model="users.email" placeholder="Email"> 
 
<input type="text" name="phoneNo" ng-model="users.phoneNo" placeholder="phone Number"> 
 
<button ng-click="pushInArray()">Add</button> 
 
<pre>{{userInfo}}</pre> 
 
</div>

0

您需要將對象傳遞給作用域函數才能保留它。

var app = angular.module('myApp',[]) 
app.controller('dropdown', function($scope, $http){ 
    $scope.userInfo = []; 

    $scope.pushInArray = function(data) { 
     var entry = (JSON.parse(JSON.stringify(data))); 
     $scope.userInfo.push(entry); 
    } 

}); 

<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script> 
<div ng-app="myApp" ng-controller="dropdown"> 

<input type="text" name="name" ng-model="users.name" placeholder="Name"> 
<input type="text" name="email" ng-model="users.email" placeholder="Email"> 
<input type="text" name="phoneNo" ng-model="users.phoneNo" placeholder="phone Number"> 
<button ng-click="pushInArray(users)">Add</button> 
<pre>{{userInfo}}</pre> 
</div> 
+0

同樣的事情,第一個值推如果我修改輸入的文本價值被推對象的值也修改 – vinox

+0

啊,我忘了補充後發生的'$ scope.users = null'以清除它作爲每@ Aayushi的回答 –

+0

感謝,它的做工精細 – vinox

1

你需要將其設置爲新值之前,清空你的users

$scope.userInfo = []; 

$scope.pushInArray = function(data) { 
    $scope.userInfo.push(data) 
    $scope.users = null; 
} 

HTML:

<input type="text" name="name" ng-model="users.name" placeholder="Name"> 
<input type="text" name="email" ng-model="users.email" placeholder="Email"> 
<input type="text" name="phoneNo" ng-model="users.phoneNo" placeholder="phone Number"> 
<button ng-click="pushInArray(users)">Add</button> 
<pre>{{userInfo}}</pre> 

這裏是工作Plnkr

+0

其工作正常,有沒有辦法做到不使用戶性能爲零 – vinox

+0

爲什麼你不想把它設置爲null? –

+0

只是我想知道有沒有什麼辦法 – vinox