angularjs select標籤中參數的傳遞

來源:互聯網
上載者:User

標籤: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標籤中參數的傳遞

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.