如何从控制器内的javascript函数调用角范围函数

时间:2022-03-14 06:45:21

I have angular controller and Javascript function in that function , i am calling angular function. I am getting error: $scope.Name is not a function, $scope.dates is not a function.

我有角控制器和Javascript函数在那个函数中,我叫角函数。我得到了错误:$scope。Name不是函数,$scope。日期不是一个函数。

     function validation() {
            $scope.pageload = true;

            $scope.Name();
            $scope.dates();  
        }

        $scope.Name = function () {
           // do something
        }

        $scope.dates = function () {
          // do something       
        }

working fine inside the controller

在控制器内正常工作。

    var MyController = function ($scope, service)
    {


       function validation() {

            $scope.pageload = true;

            $scope.Name();
         $scope.dates();

        }

       $scope.Name = function () {


            // do something
        }

     $scope.dates = function () {

            // do something

    }


});


working:


var MyController = function ($scope, service)
{
 LoginHomeService.getHomeService(function (data) {
                $rootScope.CT1SessionObj = data.CT1SessionObj;

                        validation();



                                    }, function (response) {
                                        alert(response.Message);
                                    });

   function validation() {

        $scope.pageload = true;

        $scope.Name();
     $scope.dates();

    }

   $scope.Name = function () {


        // do something
    }

 $scope.dates = function () {

        // do something




});




Not working:

    var MyController = function ($scope, service)
    {
     LoginHomeService.getHomeService(function (data) {
                    $rootScope.CT1SessionObj = data.CT1SessionObj;

                            validation();


   function validation() {

        $scope.pageload = true;

         $scope.Name();
         $scope.dates();

        }

       $scope.Name = function () {


            // do something
        }

     $scope.dates = function () {

            // do something

    }


   }, function (response) {
    alert(response.Message);
   });


   });

3 个解决方案

#1


3  

Declare $scope.Name and $scope.dates on top of validation()

宣布美元范围。名称和美元范围。验证日期()

Javascript works from top to bottom, so your functions $scope.Name and $scope.Dates do not exist 'yet'.

Javascript从上到下工作,所以函数值为$scope。名称和美元范围。日期还不存在。

Also, try not to use 'Name' as a function. Most of these words are reserved keywords.

另外,尽量不要使用“Name”作为函数。这些词大部分是保留的关键字。

var myApp = angular.module('myApp', []);

//myApp.directive('myDirective', function() {});
//myApp.factory('myService', function() {});

function MyCtrl($scope) {

    $scope.Name = function() {
    // do something
  }

  $scope.dates = function() {
    // do something       
  }

  function validation() {
    $scope.pageload = true;

    $scope.Name();
    $scope.dates();
  }
}

Fiddle: http://jsfiddle.net/Lvc0u55v/4872/

小提琴:http://jsfiddle.net/Lvc0u55v/4872/

An even better approach would be the 'John Papa style' : Y033

一个更好的方法是“约翰·帕帕帕式”:Y033

Place bindable members at the top of the controller, alphabetized, and not spread through the controller code.

将可绑定的成员放在控制器的顶部,按字母顺序排列,而不是通过控制器代码进行传播。

Why?: Placing bindable members at the top makes it easy to read and helps you instantly identify which members of the controller can be bound and used in the View.

为什么?:将可绑定的成员放在顶部可以方便地阅读,并帮助您立即识别哪些控制器的成员可以被绑定并在视图中使用。

Why?: Setting anonymous functions in-line can be easy, but when those functions are more than 1 line of code they can reduce the readability. Defining the functions below the bindable members (the functions will be hoisted) moves the implementation details down, keeps the bindable members up top, and makes it easier to read.

为什么?:在行中设置匿名函数很容易,但是当这些函数超过一行代码时,就会降低可读性。定义可绑定成员下面的函数(函数将被提升)将实现细节向下移动,将可绑定的成员保持在顶部,并使其更易于阅读。

/* avoid */
function SessionsController() {
    var vm = this;

    vm.gotoSession = function() {
      /* ... */
    };
    vm.refresh = function() {
      /* ... */
    };
    vm.search = function() {
      /* ... */
    };
    vm.sessions = [];
    vm.title = 'Sessions';
}


/* recommended */
function SessionsController() {
    var vm = this;

    vm.gotoSession = gotoSession;
    vm.refresh = refresh;
    vm.search = search;
    vm.sessions = [];
    vm.title = 'Sessions';

    ////////////

    function gotoSession() {
      /* */
    }

    function refresh() {
      /* */
    }

    function search() {
      /* */
    }
}

#2


0  

As @Harald Wiesinger mentioned declare called functions prior to calling function.

正如@Harald Wiesinger所提到的,在调用函数之前调用函数。

#3


-1  

Put validation after the scope functions

在作用域函数之后进行验证。

$scope.Name = function () {
   // do something
}

$scope.dates = function () {
  // do something       
}

function validation() {
    $scope.pageload = true;

    $scope.Name();
    $scope.dates();  
}

#1


3  

Declare $scope.Name and $scope.dates on top of validation()

宣布美元范围。名称和美元范围。验证日期()

Javascript works from top to bottom, so your functions $scope.Name and $scope.Dates do not exist 'yet'.

Javascript从上到下工作,所以函数值为$scope。名称和美元范围。日期还不存在。

Also, try not to use 'Name' as a function. Most of these words are reserved keywords.

另外,尽量不要使用“Name”作为函数。这些词大部分是保留的关键字。

var myApp = angular.module('myApp', []);

//myApp.directive('myDirective', function() {});
//myApp.factory('myService', function() {});

function MyCtrl($scope) {

    $scope.Name = function() {
    // do something
  }

  $scope.dates = function() {
    // do something       
  }

  function validation() {
    $scope.pageload = true;

    $scope.Name();
    $scope.dates();
  }
}

Fiddle: http://jsfiddle.net/Lvc0u55v/4872/

小提琴:http://jsfiddle.net/Lvc0u55v/4872/

An even better approach would be the 'John Papa style' : Y033

一个更好的方法是“约翰·帕帕帕式”:Y033

Place bindable members at the top of the controller, alphabetized, and not spread through the controller code.

将可绑定的成员放在控制器的顶部,按字母顺序排列,而不是通过控制器代码进行传播。

Why?: Placing bindable members at the top makes it easy to read and helps you instantly identify which members of the controller can be bound and used in the View.

为什么?:将可绑定的成员放在顶部可以方便地阅读,并帮助您立即识别哪些控制器的成员可以被绑定并在视图中使用。

Why?: Setting anonymous functions in-line can be easy, but when those functions are more than 1 line of code they can reduce the readability. Defining the functions below the bindable members (the functions will be hoisted) moves the implementation details down, keeps the bindable members up top, and makes it easier to read.

为什么?:在行中设置匿名函数很容易,但是当这些函数超过一行代码时,就会降低可读性。定义可绑定成员下面的函数(函数将被提升)将实现细节向下移动,将可绑定的成员保持在顶部,并使其更易于阅读。

/* avoid */
function SessionsController() {
    var vm = this;

    vm.gotoSession = function() {
      /* ... */
    };
    vm.refresh = function() {
      /* ... */
    };
    vm.search = function() {
      /* ... */
    };
    vm.sessions = [];
    vm.title = 'Sessions';
}


/* recommended */
function SessionsController() {
    var vm = this;

    vm.gotoSession = gotoSession;
    vm.refresh = refresh;
    vm.search = search;
    vm.sessions = [];
    vm.title = 'Sessions';

    ////////////

    function gotoSession() {
      /* */
    }

    function refresh() {
      /* */
    }

    function search() {
      /* */
    }
}

#2


0  

As @Harald Wiesinger mentioned declare called functions prior to calling function.

正如@Harald Wiesinger所提到的,在调用函数之前调用函数。

#3


-1  

Put validation after the scope functions

在作用域函数之后进行验证。

$scope.Name = function () {
   // do something
}

$scope.dates = function () {
  // do something       
}

function validation() {
    $scope.pageload = true;

    $scope.Name();
    $scope.dates();  
}