2017-05-26 45 views
1

AngularJS手錶不適用於下拉式更改。這裏是我的代碼,這不完全是原始代碼,我只是模擬。AngularJS手錶不適用於下拉式(選擇)更改

代碼:

<body ng-app="app" ng-controller="ctrl"> 
<select ng-model=「selectedId" ng-options="val as val.name for val in options | orderBy:'id'"> 
</select> 
<script> 
angular.module("app",[]) 
    .controller("ctrl",['$scope',function($scope){ 
    $scope.options = [ 
     {"id":1, "name":"First"}, 
     {"id":2, "name":"Second"} 
    ] 
    $scope.selectedId = {"id":1, "name":"First"} 
    $scope.$watch('selectedId’,function(selVal) { 
     console.log(selVal); 
    }); 
    }]) 
</script> 
</body> 
+1

你的代碼中有一些錯誤的字符 - 「selectedId」和「selectedId」。這只是一個壞的複製粘貼嗎? –

+1

實際代碼中存在不同的符號? – Anton

+0

@Anton檢查更新的代碼。 – SANN3

回答

0

我加入了可行的解決方案。

我已將$ scope.selectedId更改爲$ scope.filters.selectedId,並在$ scope.filters.selectedId上添加一個監視。

我不知道這是爲什麼起作用。

2

你有這麼多錯別字嘗試設置$ scope.options [0],而不是選擇[0]

angular.module("app",[]) 
 
    .controller("ctrl",['$scope',function($scope){ 
 
    $scope.options = [ 
 
     {id:1, name:'First'}, 
 
     {id:2, name:'Second'} 
 
    ] 
 
    $scope.selectedId = $scope.options[0]; 
 
    $scope.$watch('selectedId',function(selVal) { 
 
     console.log(selVal); 
 
    }); 
 
    }])
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script> 
 
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script> 
 
<html> 
 
<body ng-app="app" ng-controller="ctrl"> 
 
<select ng-model="selectedId" ng-options="val as val.name for val in options"> 
 
</select> 
 
</body> 
 
<html>

+0

檢查更新的代碼。 – SANN3

+0

那麼是什麼問題?我可以看到它是否沒有錯字,然後它可以正常工作。 @ SANN3 – Jenny

0

特別字符用於代碼而不是普通引號,並使用$scope.options[0]而不是options[0]

工作plunker here

相關問題