如何从$ scope中删除对象。变角度

时间:2016-11-30 12:03:22

标签: angularjs angularjs-directive angularjs-scope

如何从EXISTS对象中删除一个对象请帮我看下面的代码和图片

$scope.Profile.address

JS

<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>

我有3个对象,我想删除一个请帮助我     enter image description here

2 个答案:

答案 0 :(得分:1)

我认为您的代码运行正常。您在代码中缺少其他内容。 确保从UI中正确传递变量。

  <ul ng-repeat="add in Profile.addresses">
    <li ng-click="removeAddress(add)">
      {{add.address}}:{{add.phone}}
    </li>
  </ul>

在这里查看Link

应该是ng-click = &#34; removeAddress(x)&#34; not ng-click =&#34; removeAddress(x.addresses )&#34;

变化====================== ^

答案 1 :(得分: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 )

现在,如果这不起作用,或者它确实有效但UI似乎没有更新,那么你的问题可能是应用范围($ scope。$ apply())。