1

I need to hide a drop-down list when the model is empty and to show it when there are values in the model.

  1. I have a service to get the values.

    angular.module('myApp.services', ['ngResource'])
        .factory('OptionsSelect', ['$resource',
            function ($resource) {
                return $resource('http://myapi/miapp/optionselect/1', {}, {
                    query: {
                        method: 'GET',
                        params: { id: '1' },
                        isArray: false
                    }
                });
            }
        ])
    
  2. The possible result is:

    {"1":"red","5":"blue","34":"blue"}
    

or

<pre>
{}
</pre>
  1. In the controller:

    $scope.optionsSelect = OptionsSelect.query();
    
  2. The view is:

     <div class="form-group" ng-hide="isHide()">
        <select class="form-control" ng-options="key as value for (key, value) in optionsSelect">
     </div>
    

    You can view the code in jsfiddle: http://jsfiddle.net/finsi/LMJXC/38/

2
  • Your query indicates that it is expecting an array (isArray: true), but your result is not an array, it's just an object. That's going to cause an error. Commented Jun 28, 2014 at 14:44
  • You are right but is a typo, in my real code I have "isArray: false" and doesn't work. Thank you. Commented Jun 28, 2014 at 16:40

2 Answers 2

0

Hi I've created jsbin for you please have a look. You should add model to you select directive and use $promise like it is below. I hope that will help.

HTML:

 <div class="form-group" ng-show="optionsSelect">
    <select class="form-control" 
            ng-model="option.color"
            ng-options="key as value for (key, value) in optionsSelect">
 </div>

JS:

  app.controller('firstCtrl', function($scope, OptionsSelect){
    $scope.optionsSelect = {};

  OptionsSelect.query().$promise.then(function(optionSelectCollection){

    angular.copy(optionSelectCollection, $scope.optionsSelect);
  });




});
Sign up to request clarification or add additional context in comments.

13 Comments

Sorry, but don't work. If I define the model inside of the controller is ok. But when I get the data from OptionsSelect.query() not.
You can try with optionSelect.length >0. What is not working I'd always shown or always hide?
ng-show="optionsSelect.length>0" does not work either
I this case "optionSelect.length >0" always hide.
Are you sure that resources return any data?
|
0

So these selected optionsSelect objects are undefined, they stay undefined if nothing is returned and become objects if something is returned. Can you just hide them if they're undefined? Such as in this fiddle...http://jsfiddle.net/LMJXC/40/

<div class="form-group" ng-hide="optionsSelect === undefined">

and

<div class="form-group" ng-hide="optionsSelect2 === undefined">

1 Comment

Thanks you. Your solution is fine. But in my code, I haven't an undefined results, I have this: preproduccion.es/angular-demos/drop-down-list/error/img2.png. The solution is jsfiddle.net/LMJXC/41 from sylwester

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.