AngularJS: $watch for a select input

前端 未结 2 872
太阳男子
太阳男子 2021-01-20 14:52

I know we can use ng-change to solve this but I would like to understand why $watch doesn\'twork on select. Maybe I\'m doingsomething wrong but it seems I am not the only on

2条回答
  •  粉色の甜心
    2021-01-20 15:29

    I accept answer from @pankajparkar but I'd like to show my final code as in fact with the proper binding proposed by @pankajparkar I don't need a $watch anymore.

    HTML

        

    URL: {{serverSelection.server.url}}

    JS

    .controller('SettingsCtrl', function ($scope, $log, serverSelection) {
    
        //List of servers to connect to
        $scope.serverSelection= serverSelection;
    
    })
    
    
    .service("serverSelection", function() {
        var self = this;
    
        self.servers = [
            { label: 'Production', value: 1, url: 'url0' },
            { label: 'Training', value: 2, url: 'url1' },
            { label: 'Local', value: 3, url: 'url2' }
        ];
    
        self.server = self.servers[1];
    
    })
    

提交回复
热议问题