简体   繁体   中英

How to use function outside $scope ( error: function isn't defined)

I have a rzslider which takes in true or false for disabled. I want disable to be true based on a function. So I want to make it disabled:$scope.truthy

I have a function called checkDupsName() checkDupsName should return true if there is a duplicate, false otherwise. I set my $scope.truthy variable to be true if the function returns true but the issue is, when I call it outside this function ( in my slider), it's ALWAYS false.

   $scope.checkDupsName = function(Mylist) {
                var i, j, n;
                n = Mylist.length;
                for (i = 0; i < n; i++) {
                    for (j = i + 1; j < n; j++) {
                        if (Mylist[i] === Mylist[j]) {
                            return true;

                        }

                    }
                }

                return false;


            };

$scope.truthy=false;
  $scope.nameList = [];
  var Types = [];

   $scope.loadRuleList = function() {  



                PrioritizeService.getData($scope.prioritizeURL).
                then(function(response) {
                        if (response) {
                            Types = response;
                        }                              


                        for (var k = 0; k < Types.length; k++) {
                           $scope.nameList.push(Types[k].name);
                        } 



                        if($scope.checkDupsName($scope.nameList)) {
                          $scope.truthy=true;
                        }                     

            };

$scope.slider = {
                value: 1,
                options: {
                    floor: 0,
                    ceil: 3,
                    showTicks: true,
                    showTicksValues: true,
                    disabled:$scope.truthy

                }
            };

You are defining it inside of your function that is being called by then. You should move it outside and make it a function defined/declared on the scope instead and have it take the data it uses as a parameter.

// initialize it so your code does not blow up in the case of forgotten undefined or null check
$scope.nameList = []; 

$scope.loadRuleList = function() {
    var me = this;
    PrioritizeService.getData($scope.MyURL).
    then(function(response) {

        if (response) {
            Types = response;    
        }                      

        // re-init the nameList field
        me.nameList = [];
        for (var k = 0; k < Types.length; k++) {
            me.nameList.push(Types[k].name)
        }
        //check for duplicates
        var areDups = me.checkDupsName(me.nameList);
    }
}

$scope.checkDupsName = function(listToCheck) {
    var i, j, n;
    n = listToCheck.length;
    for (i = 0; i < n; i++) {
        for (j = i + 1; j < n; j++) {
            if (listToCheck[i] === listToCheck[j]) {
                return true;
            }
        }
    }    
    return false;    
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM