簡體   English   中英

AngularJS-選擇的ng模型無法正常工作

[英]AngularJS - Chosen ng-model is not working properly

當我從AngularJS選擇的localytics下拉列表中選擇一個選項時,我試圖獲得價值。 這是下拉菜單的代碼:

<select chosen style="width: 250px"
        ng-model="StudentId"
        ng-options="allStudent.StudentId as allStudent.Name for allStudent in allStudent">
        <option value="">Select</option>
</select>
<h2>Chosen with static options: {{ StudentId }}</h2>
<input type="button" value="Search" ng-click="GetStdCourseList(StudentId)">

在我的控制器中,我具有以下功能:

    $scope.GetStdCourseList = function (StudentId) {
        alert(StudentId);
    };

當我使用警報時,它將顯示“未定義”而不是顯示值我該如何解決?

實際上這是可以的,當我不使用選擇的搜索時我會獲得價值,但是當我使用“選擇”時它就不起作用了。...這是屏幕截圖:

在此處輸入圖片說明

非常感謝。

我認為您在單擊按鈕之前尚未選擇任何選項。 當我選擇該選項時,對我來說效果很好。

 var app = angular.module('myApp', []); app.controller('myCtrl', function($scope) { $scope.allStudent = [{StudentId: 0, Name: 'name1'}, {StudentId: 1, Name: 'name2'}]; $scope.GetStdCourseList = function (StudentId) { alert(StudentId); }; }); 
 <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.4/angular.min.js"></script> <div ng-app="myApp" ng-controller="myCtrl"> <select chosen style="width: 250px" ng-model="StudentId" ng-options="allStudent.StudentId as allStudent.Name for allStudent in allStudent"> <option value="">Select</option> </select> <h2>Chosen with static options: {{ StudentId }}</h2> <input type="button" value="Search" ng-click="GetStdCourseList(StudentId)"> </div> 

好吧,由於ng-model的對象是$ scope.something本身,請嘗試

更換

alert(StudentId)

成為

alert($scope.StudentId)

我不認為這種情況下您將需要參數。 只需忽略它,因為$ scope為您覆蓋了它

那會做

編輯以提高可讀性

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM