Javascript Angularjs ng change未启动
嗨,我正在开发Angularjs应用程序。我有两个下拉列表框。每当我在第一个下拉列表中选择某个内容,然后根据firstdropdown的值,将加载第二个firstdropdown(级联dropdownlistbox)。我使用Ajax调用将数据绑定到dropdownlist。 下面是我的index.html,其中有两个dropdownlistboxeJavascript Angularjs ng change未启动,javascript,angularjs,Javascript,Angularjs,嗨,我正在开发Angularjs应用程序。我有两个下拉列表框。每当我在第一个下拉列表中选择某个内容,然后根据firstdropdown的值,将加载第二个firstdropdown(级联dropdownlistbox)。我使用Ajax调用将数据绑定到dropdownlist。 下面是我的index.html,其中有两个dropdownlistboxe <select ng-model="selectedMake" ng-options="b for b in list" id="brand"
<select ng-model="selectedMake" ng-options="b for b in list" id="brand" ng-change="getModel(selectedMake)">
<option value="">-- Select a Make --</option>
</select>
<select ng-model="selectedModel" ng-options="b for b in Modellist" id="brand">
<option value="">-- Select a Model --</option>
</select>
我能够加载第一个下拉列表。在第一个下拉列表中,我在ng change事件中使用了getModel函数。此事件未触发。我也没有收到任何错误消息。我可以知道我在这里遗漏了什么吗?提前谢谢 试试这个。当使用
然后
获取响应时,应该是这样的
答复.数据
使用$scope而不是$rootScope
$scope.getModel = function (selectedMake) {
debugger;
var arrModel = new Array();
$http.get(url + 'api' + '/AutoLease/' + selectedMake + '/GetVehicleModel').then(function (response) {
$.map(data, function (item) {
arrModel.push(item.ModelName);
});
$rootScope.Modellist = arrModel;
});
另外,可能还有另一个原因,即所选的值相同,所以不会触发ng更改。如果选项值不同,请检查它们。其他人建议您使用
$scope
而不是$rootScope
但如果您必须将其用于其他理由(如共享全局变量),则必须在视图中以$root
作为前缀,如下所示
创建Plunker我认为您不需要在getModel函数中传递selectedMake。只需在函数中以$scope.selectedMake的形式访问它,它将具有所选的值,请提供任何JSFIDLE或PLUNK,以便您快速获得答案,其他人也可以轻松理解。尝试设置$scope而不是$rootScope。谢谢。请在这里找到。谢谢,但我传递了$rootScope,如下所示:angular.module('RoslpApp').controller('HomePageController',['$rootScope','$http','$translatePartialLoader','$translate','toastr','cfg',函数($rootScope,$http,$translatePartialLoader,$translate,toastr,cfg)它到达方法了吗?$rootScope.getModel没有被激发。使用$rootScope是一个错误的选择practice@NiranjanGodbole找到解决方案?@Niranjan Godbole请测试此答案谢谢,但问题是$rootScope.getModel不起作用。请查看此问题。此问题与OP问题无关(使用$rootScope而不是$Scope)但这是强烈推荐的,因为成功是不受欢迎的
$http.get(url + 'api' + '/AutoLease/' + selectedMake + '/GetVehicleModel').then(function (response) {
$.map(response.data, function (item) {
arrModel.push(item.ModelName);
});
$rootScope.Modellist = arrModel;
});
$scope.getModel = function (selectedMake) {
debugger;
var arrModel = new Array();
$http.get(url + 'api' + '/AutoLease/' + selectedMake + '/GetVehicleModel').then(function (response) {
$.map(data, function (item) {
arrModel.push(item.ModelName);
});
$rootScope.Modellist = arrModel;
});