2017-11-08 20 views
0

AngularJS selected localyticsドロップダウンからオプションを選択したときに値を取得しようとしています。ここ は、ドロップダウンのためのコードです:AngularJS - 選択されたモデルが正常に動作しない

<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); 
    }; 

は、それが代わりにどのように値 を示すの「未定義」私を見ます私はこれを解決する?

enter image description here

ありがとう:

実際のスクリーンショットされ、これはOKだと私は私が選択した検索を使用していない値を取得しますが、私が使用している場合、それはここで....働いていない「選ばれました」たくさん。

+0

モデルがStudentIdされているので、あなたはおよそ 'chosen'属性である何あなたの機能と簡単な使用する$ scope.StudentId –

+0

でのparamを無視することができますか? – Maxim

答えて

0

も、NG-モデルのオブジェクトは$自体がscope.somethingされているので、

alert(StudentId) 

alert($scope.StudentId) 

になっ置き換える

に試してみて、私はこのような場合、あなたにはないと思いますparamが必要です。 $の範囲だが、私はあなたがボタンをクリックする前に、オプションを選択していないと思う読みやすく

2

のために編集

を行いますあなた

のためにそれをカバーしているため、単にそれを無視。このオプションを選択すると、これはうまく動作します。

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>

+0

ありがとうNitheesh.But私はあなたのコードで使用されている '選択'は動作していないと思う。私のポストのスクリーンショットを確認してください。 –

+0

@SubhoGhoseこの選択された属性は何ですか? – Nitheesh

+0

まあ、それはドロップダウンからデータを保存するためのものです.... –

関連する問題