2016-06-02 6 views
2

나는 각도 함수를 호출 오전 각도 컨트롤러 및 자바 함수가 있습니다. 오류가 발생했습니다 : $ scope.Name은 함수가 아니며 $ scope.dates는 함수가 아닙니다. 스코프 기능 후 컨트롤러 내부각도 범위 함수를 호출하는 방법 컨트롤러 내부에서 자바 스크립트 함수에서

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); 
    }); 


    }); 
+1

이 코드는 모두 어디에 있습니까? 이 모든 것이 컨트롤러 안에 있습니까? 아니면 ...? – ajmajmajma

+0

somewhee –

+0

에서'validation()'을 호출하십시오. 서비스 내부에서 컨트롤러를 호출하는 중에 Plunker를 만들 수 있습니다 : –

답변

-1

넣어 유효성 검사를 제대로 작동

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

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

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

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

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

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

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

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

validation()

자바 스크립트의 상단에 $scope.Name$scope.dates 선언은 위에서 아래로 작동 , 그래서 당신의 functi ons $scope.Name$scope.Dates은 아직 존재하지 않습니다.

또한 '이름'을 함수로 사용하지 마십시오. 이 단어의 대부분은 예약어입니다.

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(); 
    } 
} 

바이올린 : http://jsfiddle.net/Lvc0u55v/4872/

더 좋은 접근 방식 '존 파파 스타일'이 될 것입니다 : 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() { 
     /* */ 
    } 
} 
+0

감사합니다. 잘 작동하는 사람 .. –

0

@Harald Wiesinger 전에 함수를 호출에 선언라는 기능을 언급 한 바와 같이 .

+0

누군가를 언급하려면 대답 대신에 주석을 사용하십시오. –

관련 문제