標籤:ng-model 想法 button 內容 問題 term angularjs 下拉 title
今天做的一個demo中需要一個下拉選擇框,並根據所選擇的內容向伺服器發送請求。
首先百度了一下angularjs關於select的使用,一種採用ng-repeat的方式。
<select ng-model="stuTerm"><option ng-repeat="term in terms" value="{{term.val}}">{{term.text}}</option></select> <p>{{stuTerm.text}}</p> <button ng-click="searchScoreY()">查詢</button>
我的想法是根據stuTerm.text向伺服器發送請求。
$scope.searchScore = function () { $http.get("userCenter/js/scoreList.json", { params: { XH: window.localStorage.stuId, XQ: stuTerm.text } }).then(function sucCall(res) { $scope.scList = res.data; }, function errCAll(res) { console.log(res); })};
但是這個時候報錯了stuTerm.text undefined。我以為是stuTerm.text沒有在範圍裡面定義的原因,就在一開始定義了$scope.stuTerm = "";但是這時候stuTerm還是等於空。即使select選擇了新的東西,<p>{{stuTerm.text}}</p>也更新了。$scope.stuTerm還是空。
誒?ng-model不是$scope <-> view的雙向繫結麼,為什麼view更新了,$scope卻擷取不到呢?
我覺得應該是ng-repeat會建立一個新的子範圍。而$scope範圍擷取不到stuTerm的內容。所以我採用了在searchScore()中傳入stuTerm.val進行調用。
<button ng-click="searchScoreY(stuTerm.val)">查詢</button>
$scope.searchScore = function (t) { $http.get("", { params: { XH: window.localStorage.stuId, XQ: t } }).then(function sucCall(res) { $scope.scList = res.data; }, function errCAll(res) { console.log(res); })};
嗯,這回是對了。上面關於範圍的問題如果有大佬看到,歡迎大佬指點啊!
angularjs select標籤中參數的傳遞