簡體   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?

我有一個選擇下拉列表選項,這些選項構成了構成我的數據庫的國家/地區,我希望成功選擇一個國家/地區或更改后,調用一個函數,該函數可以通過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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM