简体   繁体   English

AngularJs:我有一个选择下拉列表,它在更改时仅调用一次函数。 每当更改时如何使它调用函数?

[英]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?

I have a select drop-list options that populated countries form My database and I want on successful selection of a country/or change to call a function that retrieves regions based on the selected country by ng-change directive. 我有一个选择下拉列表选项,这些选项构成了构成我的数据库的国家/地区,我希望成功选择一个国家/地区或更改后,调用一个函数,该函数可以通过ng-change指令基于所选国家/地区检索区域。 So far so good the countries can be retrieved and populated in the select drop-list option control. 到目前为止,可以在选择下拉列表选项控件中检索并填充国家/地区。 Now: what I don't seem to understand is My directive only executes once and return the regions based on the selected option but if I repeat or change the country without refreshing the browser I get TypeError: v2.MyregionFunction is not a function . 现在:我似乎不明白的是,我的指令仅执行一次并根据所选选项返回区域,但是如果我重复或更改国家/地区而不刷新浏览器,我将得到TypeError:v2.MyregionFunction不是一个函数 the MyregionFUnction references to the method I call to retrieve the regions its name is nationalRegions . MyregionFUnction引用了我调用的方法来检索名称为nationalRegions的区域。 Bellow is the code in action: 波纹管是起作用的代码:

In index.html 在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>

Now in appModule.js 现在在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;
    }


}

});

And finally app.js 最后是app.js

app.controller('ctlOptions',['$scope','miscellaneous',function ctlOptions($scope,miscellaneous){

$scope.userCountries=miscellaneous.fetchCountries($scope);

$scope.nationalRegions=function(idCountry){

        miscellaneous.fetchRegions($scope,idCountry);
    }

}]);

That is it can I get assistance where I am making a mistake please because as I said the codes function normally only once and problems arise when I re-select another country and sorry I have not included the appBackend.php file as to it there is no problems I hope it wont affect the debuging and if there seems to be something wrong please let Me know as its My first post here I may have many typo of the code but as it is on My local machine its perfect written without counting the problem in hand. 那是我在哪里出错时可以得到帮助,因为正如我所说的,代码只能正常运行一次,当我重新选择另一个国家时出现问题,对不起,我没有包含appBackend.php文件。没问题,我希望它不会影响调试,如果出现问题,请让我知道。我在这里的第一篇文章中我可能有很多打字错误,但由于它是在我的本地计算机上编写的,所以它不算问题在手里。

there is a solution that was even hard to trace in my code because it was perfect I'd say :) with a little exception that angularjs wont tell you either. 有一种解决方案甚至很难在我的代码中追踪 ,因为它是完美的,我会说:),但有一点例外,即angularjs也不会告诉您。 Its here answered by Aaron Gray 亚伦·格雷 在这里回答

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如果我有一个分配给函数调用值的变量,如果函数调用的参数发生更改,是否可以更新该变量? - 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? 如何在更改值而不是单击按钮时运行函数? - How can I make my function run when a value is changed instead of when the button is clicked? 如何在视图更改时从AngularJS调用JQuery函数 - How to call a JQuery function from AngularJS when a view is changed 每当用户更改视口大小时,调用一个函数 - Call a function whenever the viewport size is changed by user 如何让这个按钮只调用一次函数? - How do I make this button to call the function only once? 仅在所有请求完成后如何才能调用函数? - How can I call a function only once all requests have finished? 当我登录/打开应用程序时,如何只调用一次该函数 - How to call the function only once, when i make a login/open app 我怎样才能仅在一次循环后调用此函数? - How can I call this function only once yet loop as needed? 我怎样才能在 typescript 中只调用一次 function - How can I call a function only once in typescript 更改此选择框后如何触发功能? - How to trigger a function when this select box changed?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM