[英]If I have a variable, assigned to the value of a function call, can that variable be updated if the function call's parameters are changed?
[英]AngularJs: I have the select droplist that calls a function only once when changed. How can I make it call the function whenever it is changed?
我有一个选择下拉列表选项,这些选项构成了构成我的数据库的国家/地区,我希望成功选择一个国家/地区或更改后,调用一个函数,该函数可以通过ng-change
指令基于所选国家/地区检索区域。 到目前为止,可以在选择下拉列表选项控件中检索并填充国家/地区。 现在:我似乎不明白的是,我的指令仅执行一次并根据所选选项返回区域,但是如果我重复或更改国家/地区而不刷新浏览器,我将得到TypeError:v2.MyregionFunction不是一个函数 。 MyregionFUnction引用了我调用的方法来检索名称为nationalRegions的区域。 波纹管是起作用的代码:
在index.html中
<!DOCTYPE html>
<html ng-app="options" lang='en'>
<head>
<title>Select Countries App:</title>
<script src="appModule.js"></script>
<script src="app.js"></script>
</head>
<body ng-controller="ctlOptions">
<label>Country:</label>
<select ng-model="userDomain" ng-options="location.otherName for location in userCountries" ng-change="nationalRegions(userDomain.idCountry)">
</select>
<label>Region:</label>
<select ng-model="regionDomain" ng-options="region.regionName for region in nationalRegions">
</body>
</html>
现在在appModule.js中
var app=angular.module("options",[]);
app.factory('miscellaneous',function(){
return{
uiRequest:function(scope,inputString,callBack){
$.ajax({
type: "POST",
url:"appBackend.php",
data: {inputString:inputString},
dataType: "json",
success : function(jsonResponce){
if(callBack && typeof ( callBack )== "function")
{
callBack(jsonResponce);
scope.$apply();
}
},
error:function(XMLHttpRequest, textStatus, errorThrown){
scope.colorCode="danger";
scope.Error="Sorry, there seems to be some problem in your request. Please crosscheck provided informations.";
scope.$apply();
}
})
},
fetchCountries:function(scope){
var inputs={Controller:"service",Action:"userCountries"};
var inputString = JSON.stringify(inputs);
this.uiRootRequest(scope,inputString,function(responced){
scope.userCountries= responced;
scope.userDomain=scope.userCountries[0]
});
return scope.userDomain;
},
fetchRegions:function(scope,idCountry){
var inputs={idCountry:idCountry,Controller:"service",Action:"nationalRegions"};
var inputString = JSON.stringify(inputs);
this.uiRootRequest(scope,inputString,function(responced){
scope.nationalRegions= responced;
scope.regionDomain=scope.nationalRegions[0];
});
return scope.regionDomain;
}
}
});
最后是app.js
app.controller('ctlOptions',['$scope','miscellaneous',function ctlOptions($scope,miscellaneous){
$scope.userCountries=miscellaneous.fetchCountries($scope);
$scope.nationalRegions=function(idCountry){
miscellaneous.fetchRegions($scope,idCountry);
}
}]);
那是我在哪里出错时可以得到帮助,因为正如我所说的,代码只能正常运行一次,当我重新选择另一个国家时出现问题,对不起,我没有包含appBackend.php文件。没问题,我希望它不会影响调试,如果出现问题,请让我知道。我在这里的第一篇文章中我可能有很多打字错误,但由于它是在我的本地计算机上编写的,所以它不算问题在手里。
有一种解决方案甚至很难在我的代码中追踪 ,因为它是完美的,我会说:),但有一点例外,即angularjs也不会告诉您。 亚伦·格雷 在这里回答
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.