2016-11-30 47 views
0

如何從$scope.Profile.address對象中刪除一個對象,請幫我看看下面的代碼和圖像

<tr ng-repeat="x in Profile.addresses"> 
    <td><input type="text" class="form-control" id="inputDefault" ng-model='x.site_name ' name='site_name'></td> 
    <td><input type="text" class="form-control" id="inputDefault" ng-model='x.street_address ' name='street_address'></td> 
    <td><input type="text" class="form-control" id="inputDefault" ng-model='x.city ' name='city'></td> 
    <td><input type="text" class="form-control" id="inputDefault" ng-model='x.state ' name='state'></td> 
    <td><input type="text" class="form-control" id="inputDefault" ng-model='x.country ' name='country'></td> 
    <td><input type="text" class="form-control" id="inputDefault" ng-model='x.zip_code ' name='zip_code'></td> 
    <td><input type="text" class="form-control" id="inputDefault" ng-model='x.phone_number ' name='phone_number'></td> 
    <td><a href="" data-toggle="tooltip" title="Remove Address" ng-click="removeAddress(x.addresses)"><i class="fa fa-close text-danger" aria-hidden="true"></i></a></td> 
</tr> 

JS

$scope.removeAddress = function(address) { 

    var index = $scope.Profile.addresses.indexOf(address); 

    if (index != -1) 
     $scope.Profile.addresses.splice(index, 1); 
    console.log($scope.Profile.addresses); 
}; 

我有3個對象,我想刪除一個,請幫我 enter image description here

+0

所以你有你的代碼有任何錯誤?我認爲你正試圖刪除你的數組中的dup –

+0

你試過用'delete $ scope.Profile.address'嗎? – manzapanza

+0

我想從我的數組中刪除一個對象我正在顯示html –

回答

1

我認爲你的代碼工作正常。您在代碼中缺少其他內容。 確保您從UI正確傳遞變量。

看一看這裏Link

應該納克單擊= 「removeAddress(X)」沒有NG-點擊= 「removeAddress(x.addresses)」

改變======================^

+0

感謝它的工作:) –

0

只是爲了它,而不是拼接出數組中的變量,嘗試創建和設置一個新的數組。

因此,而不是「查找索引,然後拼接」就地陣列編輯,做這樣的事情:

// newArray will contain all objects except the one you want removed 
var newArray = $scope.profile.addresses.filter (function (d) { 
    return d.address !== address; 
}); 

// newArray should trace out as you'd expect, unless filtering didn't find a match 
console.log (newArray); 
// If all is well, replace the old array 
$scope.profile.addresses = newArray; 
// $scope.profile.addresses should be the same as newArray 
console.log ($scope.profile.addresses) 

現在,如果不工作,或者它的工作,但用戶界面不會似乎正在更新,那麼你的問題可能是應用範圍($ scope。$ apply())。