13

I am working on an angularjs project and i have a problem with the ngModel not binding within the select.But the same concept is working in another select tag and in the same html page. Below is the code.

  <select ng-model="selectedFont" 
          ng-options="font.title for font in fonts" 
          ng-change="onFontChange()">
  </select>

onFontChange() function is placed in the controller.

Anyone help is highly appreciable...Thanks in advance.

5
  • 1
    kindly share complete code or share jsfiddle demo Commented Apr 29, 2013 at 6:30
  • i don't know about how to use jsfiddle Commented Apr 29, 2013 at 6:45
  • Can you post the code for onFontChange()? Commented Apr 29, 2013 at 6:50
  • 1
    Here's an example fiddle for AngularJS: jsfiddle.net/3y5Pw Commented Apr 29, 2013 at 6:51
  • 1
    Most likely you're trying to get fonts from a different scope. Try to print it under the select: {{ selectedFont }}{{ fonts }} or install Batarang extension for Chrome browser. Commented Apr 29, 2013 at 7:17

2 Answers 2

21

Based on Tony the Pony's fiddle :

<div ng-app ng-controller="MyCtrl">
    <select ng-model="opt"
            ng-options="font.title for font in fonts"
            ng-change="change(opt)">
    </select>

    <p>{{opt}}</p>
</div>

With a controller:

function MyCtrl($scope) {
    $scope.fonts = [
        {title: "Arial" , text: 'Url for Arial' },
        {title: "Helvetica" , text: 'Url for Helvetica' }
    ];
    $scope.change= function(option){
        alert(option.title);
    }
}

http://jsfiddle.net/basarat/3y5Pw/43/

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

1 Comment

You can also use $scope.$watch() as stated at: stackoverflow.com/questions/15727219/…
3

First create data (can be local or from server) in Controller. And initialize the default value, which force the default item selected in HTML form.

// supported languages
$scope.languages = ['ENGLISH', 'SPANISH', 'RUSSIAN', 'HINDI', 'NEPALI'];
// init default language
$scope.language = 'ENGLISH';

Now in your HTML form

<select class="form-control" ng-model="language">
    <option ng-repeat="language in languages">{{language}}</option>
</select>


The screenshot is here (note bootstrap CSS is used and not included here).

enter image description here


You can do a test in controller, whether the change is working

$scope.$watch('language', function (newVal, oldVal) {
    console.log(oldVal + " -> " + newVal);
});

ENGLISH -> RUSSIAN

RUSSIAN -> SPANISH

SPANISH -> RUSSIAN

Hope this is helpful. Thanks!

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.