3

ng-change="changeStudentStatus();" の呼び出し オプションを選択する前に、ロード時に機能します。

<md-select ng-model="ctrl.newStudentStatus" ng-change="changeStudentStatus();" ng-if="ctrl.studentStatusList" >
   <md-option class="ss-options" ng-value="item.display_name" ng-repeat="item in ctrl.studentStatusList" ng-selected="item.id == ctrl.statusId">{{item.display_name}}</md-option>
</md-select> 

脚本 :

$scope.changeStudentStatus = function(){
     //some logic  
};

DropDown を変更するときに呼び出す必要があります。私のスクリプトで何が問題なのですか

4

3 に答える 3

1

オプションを選択する前に、ページが読み込まれるたびに changeStudentStatus() を呼び出します。

<md-select ng-model="ctrl.newStudentStatus" ng-change="changeStudentStatus();" ng-if="ctrl.studentStatusList" >
   <md-option class="ss-options" ng-value="item.display_name" ng-repeat="item in ctrl.studentStatusList" ng-selected="item.id == ctrl.statusId">{{item.display_name}}</md-option>
</md-select> 

一時的な解決策で問題を解決しました。ユーザーが値を変更したときにのみ changeStudentStatus を呼び出す必要があるため、一時変数を作成して以前の値を保存し、値が変更されたときにのみロジックを実行できます。

脚本:

$scope.newValue  =ctrl .newStudentStatus;
        $scope.changeStudentStatus = function(){
              if($scope.oldVlaue === $scope.newValue)
              {
                //some logic  
              }
        };
于 2017-01-12T08:30:07.967 に答える
-3

変化する

ng-change="changeStudentStatus();" 

ng-change="ctrl.changeStudentStatus();"

違いは、コード内で というメソッドを呼び出すことですchangeStudentStatus()。このメソッドはコントローラーでは使用できません。

ctrl他のメソッド/プロパティが呼び出されているため、コントローラーをエイリアスしたと想定しましたctrl.*

メソッドctrl.changeStudentStatus()を呼び出すと、コントローラーに設定されているメソッドが実際に呼び出されます。

于 2016-12-28T07:56:11.633 に答える
-3

メソッド ngModelCtrl.$setViewValue を使用すると、ng-change 式が自動的に評価されます。

angular.module("myApp").directive("mdselect", function(){  return {
require:"^ngModel", // this is important, 
scope:{      ... // put the variables you need here but DO NOT have a variable named ngModel or ngChange 
}, 
link: function(scope, elt, attrs, ctrl){ // ctrl here is the ngModelCtrl
  scope.setValue = function(value){
    ctrl.$setViewValue(value); // this line will automatically eval your ng-change
  };
}};});
于 2016-12-28T08:13:43.713 に答える