简体   繁体   中英

How to set default selected option value taken from server side (php) in select drop down using angularjs

In the code below select drop down working fine, but now I need to set the default option of the select from the response options of server side.
That matching data is also retrieved from server side (PHP). What's wrong with my code? Is there a correct way in angularjs or any is there any other option available? I referred more than stack overflow's. but in all that issues only static values are solved.not from server side..
Thanks advance

 **My HTML**    
<select ng-model="frameedit.brand_name" ng-options="brand.id as brand.brand_name for brand in frame_product_options.brand_type">
</select>

My Controller

$scope.frame_product_options_get=function(){  
    $http.post("ajax/frame_product_select_data.php").
        success(function(data, status, headers, config) {  
            $scope.frame_product_options=data;  
            $scope.frameedit.brand_name=data.brand_type_match[0]; //just try to assign.but it's not work..
        });  
}

my json console data from server side(PHP)

  Overall Data:  

    brand_type[Object { id="1", brand_name="RAY-BAN"}, Object { id="2", brand_name="Bolle"}, Object { id="3", brand_name="vogue-brand"}]

want to match with the below data:  

brand_type_match[Object { id="2", brand_name="Bolle"}]

You are using the select as label for value in array syntax for the ng-options directive.

The main point here is that "select" should match the expression provided for ng-model .

In your ng-model you have provided the "brand name" but your select is the "brand id".

Either use the select as the brand name or change your ng-model to use brand id instead. That is use either -

<select ng-model="frameedit.brand_name" ng-options="brand.brand_name
    as brand.brand_name for brand in frame_product_options.brand_type">
</select>

OR

<select ng-model="frameedit.id" ng-options="brand.id
    as brand.brand_name for brand in frame_product_options.brand_type">
</select>

Note that the former requires that the select , that is the "brand_name" parameter to be unique.

It should be

$scope.frameedit.brand_name = data.brand_type_match[0].id;

since the select part of ng-options is brand.id which is bound to ng-model , so setting that default value from controller must be compatible with the configuration on the view.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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