gpt4 book ai didi

javascript - 如何在angularjs中获取修改后的对象

转载 作者:行者123 更新时间:2023-12-03 04:07:10 24 4
gpt4 key购买 nike

我有一个要求,我需要在对象中单独发送修改后的值。

以下是我的目标:

{
"Code": 200,
"ErrorMessage": null,
"Result": {
"Locations": [{
"LocationName": "Location 1",
"Address": "XYZ",
"City": "Houston",
"State": "TEXAS",
"StateCode": "TX",
"Zipcode": "75201"
},
{
"LocationName": "Location 2",
"Address": "ABC",
"City": "Germantown",
"State": "CALIFORNIA",
"StateCode": "CA",
"Zipcode": "90001"
}]
}
}

我使用 ng-repeat 来显示具有输入字段的数据。现在,如果我修改该位置对象中的位置 1。我只想发送位置 1 的详细信息。

是否可以在 Angular 中做到这一点?我是 Angular 新手。

最佳答案

你可以使用ng-change来获取修改后的对象

angular.module("app",[])
.controller("ctrl",function($scope){

$scope.changeItem = function(item){
console.log(item.LocationName)
}

$scope.items = {
"Code": 200,
"ErrorMessage": null,
"Result": {
"Locations": [{
"LocationName": "Location 1",
"Address": "XYZ",
"City": "Houston",
"State": "TEXAS",
"StateCode": "TX",
"Zipcode": "75201"
},
{
"LocationName": "Location 2",
"Address": "ABC",
"City": "Germantown",
"State": "CALIFORNIA",
"StateCode": "CA",
"Zipcode": "90001"
}]
}
}
})
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.22/angular.min.js"></script>
<div ng-app="app" ng-controller="ctrl">
<div ng-repeat="item in items.Result.Locations">
<input ng-model="item.LocationName" ng-change="changeItem(item)"/>
</div>
</div>

关于javascript - 如何在angularjs中获取修改后的对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44499334/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com