简体   繁体   English

如何在AngularJS中同步select和GET-param?

[英]How synchronize select and GET-param in AngularJS?

I have some filter on the page and I need to synchronize value of select element with GET-param. 我在页面上有一些过滤器,我需要将select元素的值与GET-param同步。

<div ng-app="TourSearchApp">
    <form ng-controller="SearchFilterCtrl">
       <select ng-model="country" ng-options="item.id as item.name for item in countries">
            <option value="">Choose country</option>
        </select>
    </form>
</div>

<script>
    var app = angular.module('TourSearchApp', []);

    app.controller('SearchFilterCtrl', ['$scope', '$http', '$location', function($scope, $http, $location) {        
        $http.get('/api/countries/').success(function(data) {
            $scope.countries = data;
        });

        $scope.country = $location.search()['country'];  // (1)
        $scope.$watch('country', function(newValue) {  // (2)
            $location.search('country', newValue);
        });
    }]);
</script>

String (1) and (2) makes synchronization of $scope.country and GET-param country. 字符串(1)和(2)使$ scope.country和GET-param country同步。 Everything works. 一切正常。 But when page loads with some GET-param it does not apply to SELECT. 但是,当页面加载某些GET参数时,它不适用于SELECT。 Ie select element stays unselected. 即选择元素保持未选择状态。 Why? 为什么?

You should search that parameter from the countries array and then set that object id for country ng-model 您应该从国家数组中搜索该参数,然后为国家ng-model设置该对象ID

$http.get('/api/countries/').success(function(data) {
    $scope.countries = data;
    if(country)
      $scope.country = ($filter('filter')($scope.countries ,{ name: $location.search()['country'] }))[0].id;
});

OR 要么

You should pass id of country in URL so that assigned value will get properly binded to the ng-options model value and you will get you value prepopulated inside drop down, you may need to use track by id inside ng-options 您应该在URL中传递国家/地区的id ,以便将分配的值正确绑定到ng-options模型值,并在下拉列表中预先填充值,您可能需要在ng-options使用track by id

ng-options="item.id as item.name for item in countries track by id"

Here is a resulting working code 这是生成的工作代码

<div ng-app="TourSearchApp">
    <form ng-controller="SearchFilterCtrl">
       <select ng-model="country" ng-options="item.id as item.name for item in countries">
            <option value="">Choose country</option>
        </select>
    </form>
</div>

<script>
    var app = angular.module('TourSearchApp', []);

    app.controller('SearchFilterCtrl', ['$scope', '$http', '$location', '$filter', function($scope, $http, $location, $filter) {        
        $http.get('/api/countries/').success(function(data) {
            $scope.countries = data;
            // validate raw GET-param
            $scope.country = $filter('filter')(data, {id: $scope.country}).length ? $scope.country : null;
        });

        // convert string to number!
        $scope.country = +$location.search()['country'];
        $scope.$watch('country', function(newValue) {
            $location.search('country', newValue);
        });
    }]);
</script>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM