Angularjs ng在选择前调用加载更改函数
在选择选项之前,在加载时调用ng change=“changeStudentStatus();”函数Angularjs ng在选择前调用加载更改函数,angularjs,material-design,angularjs-select,Angularjs,Material Design,Angularjs Select,在选择选项之前,在加载时调用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
<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>
它应该在使用更改下拉列表时调用。我的脚本有什么问题吗
ng-change="changeStudentStatus();"
到
区别在于,在代码中,您调用了一个名为changeStudentStatus()
的方法。此方法在控制器上不可用
我假设您将控制器设置为ctrl
,因为您的其他方法/属性正在使用ctrl.*
当您调用方法
ctrl.changeStudentStatus()
时,实际上是调用在控制器上设置的方法 使用方法ngModelCtrl.$setViewValue时,将自动计算ng更改表达式
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
};
}};});
它在选择选项之前每次加载页面时都调用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>
使用ng show而不是ng-if。如果您使用的是旧版本,则更新可能会起作用(),这是我在遇到类似问题时发现的。当时找不到一个实际的解决方案,所以我将使用您的工作答案:)它们之间有什么区别。?可以解释我该如何解决这个问题。`ng selected=“item.id==ctrl.statusId”`当我删除它时,它工作正常,但当保留这个调用控制器时。不确定是谁对这个答案投了反对票(并对问题投了赞成票)。但我的回答对给定的问题是有效的。话虽如此。你应该提供更多的信息。你给了我们一些关于你的控制器的信息,你想要达到的目标等等。你从我的问题中了解到,它已经自动调用了。我只想在用户选择下拉菜单时执行ng chang。希望你能理解
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
};
}};});
<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.newValue =ctrl .newStudentStatus;
$scope.changeStudentStatus = function(){
if($scope.oldVlaue === $scope.newValue)
{
//some logic
}
};