将ng-repeat子对象绑定到另一个子对象(zipcode + city!)

时间:2013-07-18 19:34:12

标签: angularjs angularjs-scope angularjs-ng-repeat angularjs-controller

主要是伪代码所以请不要介意是否有拼写错误。我正在试图弄清楚如何为我的控制器方法访问address.City.ZipCode的值;因为这只是地址迭代中的一个孩子(标记为(???))。目标是通过邮政编码过滤城市。

看起来很简单,但它确实让我难过。也许有一个类似$ scope.address.City.ZipCode的语法数组? ng-repeat的原因是我打算在每个帐户中都有一个大的地址列表。

谢谢你的朋友

view.html

<div ng-repeat="address in addresses">

    <select ng-model="address.City.Id" ng-options:(cityList.id as cityList.name)>
    </select>

    <input type="text" ng-model="address.City.ZipCode" ng-change="getCityByZip()" />

</div>

controller.js

$scope.getCityByZip = function () {

   $http.get("http://localhost/GetCitiesByZipCode?zip=" + (???) ).success(function (data) {
            $scope.cityList = data; 
}

1 个答案:

答案 0 :(得分:1)

你可以通过多种方式解决这个问题,让我只展示其中一个虚拟代码

<input type="text" ng-model="address.City.ZipCode" ng-change="getCityByZip($index)" />

$scope.getCityByZip = function (index) {

   $http.get("http://localhost/GetCitiesByZipCode?zip="+$scope.addresses[index].City.zipCode ).success(function (data) {
            $scope.cityList = data; 
}
相关问题