AngularJS ngModel directive in select

后端 未结 2 868
梦毁少年i
梦毁少年i 2020-12-31 08:52

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 a

2条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-31 09:14

    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

    
    


    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!

提交回复
热议问题