0

페이지로드시 angularjs 함수 (DeptSpecific)를 호출하고 ID를 하드 코딩 된 매개 변수로 전달하려고합니다. 그리고 그것은 ng-init = "DeptSpecific ('1')"을 통해 전달됩니다. 나는 angularjs를 배우고 있습니다. 어떻게 함수를 호출하고 페이지 로딩시 매개 변수를 전달할 지 제안 해주세요. 만약 당신이 왜 ng-init을 사용했는지 생각하고있다. 이것에 대한 특별한 이유가 없다.하지만 잘못되었을 수도있다. 그러나 함수가 잘 호출된다. 디버거 (f12)에서 볼 수 있지만 $ scope.si는 일치하지만 신분증.MVC 4와 angularjs를 실제로 사용하고 있습니다. 페이지로드시 각도 js 함수를 호출하려고합니다.

$scope.DeptSpecific = function (ID) { 

    var BelongsToThisDepartment = []; 
    $http.get('/Department/getDept').success(function (response) { 
     $scope.departments = $scope.$eval(response); 
    }); 
    angular.forEach($scope.departments, function (item1) { 
     if (item1.ID == ID) { 
      BelongsToThisDepartment.push(item1); 
     } 
    }) 

    $scope.s = $scope.$eval(angular.toJson(BelongsToThisDepartment)); 

    // console.log(JSON.stringify($scope.s)); 

} 




<div ng-app="MyApp"> 
    <div ng-controller="MyController"> 
     <table class="tableData" border="0" cellspacing="0" cellpadding="0" ng-init="DeptSpecific('1')"> 
      <thead> 
      <tr> 
       <th></th> 
       <th>ID</th> 
       <th>NAME</th> 
       <th>LOCATION</th> 
      </tr> 
      </thead> 
      <tbody ng-repeat="O in s"> 
       <tr ng-class-even="'even'" ng-class-odd="'odd'"> 
       <td class="CX" ng-click="student(O.ID)"><span>+</span></td> 
        <td>{{O.ID}}</td> 
        <td>{{O.Name}}</td> 
        <td>{{O.Location}}</td> 
       </tr> 
+0

여기서 MyController 코드는 무엇입니까? 당신이 MyController에 $ scope를 삽입하지 않는다는 솔기가 있습니다. – Merlin

+0

아니요 ... 나는이 포스트에서 내 전체 코드를 복사하여 붙여 넣을 수 없다고 덧붙였습니다. –

+0

컨트롤러의 코드를 추가 할 수 있습니까 – Merlin

답변

0

NG-초기화를 사용하여 코드를 보는 것은 괜찮지 만, 당신의 $scope.departments.success 방법 외부에서 액세스 할 수 없습니다.

angular.module('MyApp') 
.controller('MyController', ['$scope', function($scope) { 
    $scope.DeptSpecific = function (ID) { 
     var BelongsToThisDepartment = []; 
     $http.get('/Department/getDept') 
     .success(function (response) { 
      $scope.departments = $scope.$eval(response); 

      angular.forEach($scope.departments, function (item1) { 
      if (item1.ID == ID) { 
       BelongsToThisDepartment.push(item1); 
      } 
      }) 
      $scope.s = $scope.$eval(angular.toJson(BelongsToThisDepartment)); 
      console.log($scope.s); 
     }); 
    } 
}]); 

는 지금 당신을 위해 작동하는 경우 그러나 당신은 또한 .success$scope.s 외부에 액세스 할 수있게하려면,

함수를 작성하여 .success에 반환 된 값을 전달하고 수행 할 작업을 수행 할 수 있습니다.

.success(function(response) { 
    callAFunction(response); 
}