0
<select  class="select" data-ng-model="sites.webSites.addable.languages"
ng-options="language as language.label for language in languages" >
                                                    </select>

$scope.languages = [
             {
                 value : 'lang_en',
                 label : 'English-en'
             },
             {
                 value : 'lang_es',
                 label : 'Spanish-es'
             },
             ];

Clicking on a button I have to make the item selected

I have tried like this

var sitevalue = 'lang_es'//dynamic

$('.select').val(sitevalue);

But it is not working ,please suggest.

4 Answers 4

3

Change your select to something like this:

<select data-ng-model="sites.language"
    ng-options="language.value as language.label for language in languages" ></select>

Then this should work inside your controller:

sites.language = 'lang_es'

Here is a working jsfiddle

Update:

When you are writing language as language.label for ... you are telling angular to map ng-model to object like { value : '...', label : '...' }. Which would be tracked per reference, means you have to assign to ng-model exacltly the same instance that you specified. Here is jsfiddle from angular team illustrating this.

You can use simple assignments to value propery to cange selected value by using this inside ng-options: language.value as language.label for ...

And you don't want to write jquery selectors or manipulate DOM, try avoid $('.select').val(sitevalue); when using angular. Use native js-models instead and rely on framework to do the rest.

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

Comments

1

Your model should be set to the exact object (by reference) that you would like to select, not only to the value.

Make you HTML like this:

  <body ng-controller="TestCtrl">
    <select  class="select" ng-options=" language.label for language in languages" ng-model="selectedValue"></select>
    <button ng-click="selectedValue = defaultValue">Select Default</button>
  </body>

and your controller like this:

function TestCtrl($scope) {
  $scope.languages = [
   {
       value : 'lang_en',
       label : 'English-en'
   },
   {
       value : 'lang_es',
       label : 'Spanish-es'
   },
   ];
   $scope.selectedValue = {};
   $scope.defaultValue = $scope.languages[1];
}

This will do the job. Working plunkr: http://plnkr.co/edit/8HrQUGMx4jGmJv3fbdDq?p=preview

Comments

0
$scope.sites.webSites.addable.languages = 'lang_es';

selected value is associated with data-ng-model

Comments

0
  1. ng-options should be "language.value as language.label for language in languages".

  2. data-ng-model (or just ng-model) should be "selectedLanguageValue".

  3. In the controller, get the value of $scope.selectedLanguageValue.

Comments

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.