Angular Js No default filtering occurs in ng-repeat

I am trying to filter the values ​​of an array when the page loads, setting a default value in the selection field, but it does not work [when changing the selection field it will filter]

Json

[{
        location_id: "2",
        location_name: "location2",
        price: "40",
        product_id: "7",
        product_name: "Burger",
        quantity: "2"
    }, {
        location_id: "3",
        location_name: "location3",
        price: "40",
        product_id: "6",
        product_name: "Dosa",
        quantity: "6"
    }, {
        location_id: "4",
        location_name: "location4",
        price: "40",
        product_id: "5",
        product_name: "cola",
        quantity: "16"
    }
]

html for Select Box

<select name="location" class="locationFilter col-md-3 col-md-push-9 col-xs-
12" ng-model="location.location_id">
   <option value="">--Select location--</option>
   <option ng-repeat="item in locationsArray" value="{{item.location_id}}" 
 ng-selected= "item.location_id == defaultLocation">{{item.location_name}}  
 </option>
</select>

html to enumerate array

<div class="col-md-3 itemList" ng-repeat="item in productArray | 
filter:location">{{item.product_name}}</div>

In the controller, I define a default value for choosing a location, as shown below

$scope.defaultLocation=3;

Any help would be greatly appreciated

+4
source share
1 answer

Use ng-value , not the value in your option:

[] , , ngModel ( )

ngModel, ng .

<select name="location" class="locationFilter col-md-3 col-md-push-9 col-xs-
12" ng-model="location.location_id">
    <option value="">--Select location--</option>
    <option ng-repeat="item in locationsArray" ng-value="item.location_id">{{item.location_name}}
    </option>
</select>

, location.location_id ng-model, ($scope.defaultLocation), :

$scope.location = {location_id: 3};

ng-selected, ng-model ng-value .

, , : http://plnkr.co/edit/6AXSA8j06gX5yju9a71y?p=preview

+1

Source: https://habr.com/ru/post/1684368/


All Articles