2016-10-20 6 views
0

同じコントローラーの中で繰り返し使用する必要があるページ作成コードがあり、それを関数内に入れることを考えました。しかし、私は期待どおりに動作していません。ある値が定義されていないというエラーを常に送信します。

これをどのように達成するのですか?

(function() { 
    'use strict'; 

    angular 
     .module('efutures.hr.controllers.creation', []) 
     .controller('UserCreateController', UserCreateController); 

    UserCreateController.$inject = ['$scope', '$location', '$rootScope', '$http', 'deptService', 'DeptNameService','EmployeeService']; 

    function UserCreateController($scope, $location, $rootScope, $http, deptService, DeptNameService, EmployeeService) { 

     (function initController() { 
      deptService.getdepts(function (res) { 

       $scope.depts = JSON.parse(res.data); 
      }); 

      EmployeeService.GetEmpDetails(function (res) { 
       $scope.FilterDetails = JSON.parse(res.data); //This is the variable that I need inside the function.    

       $scope.PaginationTrigger($scope.FilterDetails); //CODE APPLIED HERE 

      }); 
     })(); 

     $scope.reset = function() { 

      $('#depts').val('').trigger('change.select2'); 
      EmployeeService.GetEmpDetails(function (res) { 
       $scope.FilterDetails = JSON.parse(res.data); 
      }); 
     }; 

     $scope.paginationTrigger =function(details){ //This method is used to control the pagination 
      $scope.nums = ["10", "20", "30"]; 
       $scope.viewBy = $scope.nums[0]; 
       $scope.totalEmployees = $scope.details.length; 
       $scope.currentPage = 1; 
       $scope.itemsPerPage = $scope.viewBy; 
       $scope.maxSize = 5; 

       $scope.setPage = function (pageNo) { 
        $scope.currentPage = pageNo; 
       }; 

       $scope.pageChanged = function() { 
        console.log('Page changed to: ' + $scope.currentPage); 
       } 

       $scope.setEmployeesPerPage = function (num) { 
        $scope.itemsPerPage = num; 
        $scope.currentPage = 1; 
       } 
     } 

     $scope.DropdownSelected = function (value) { 
      console.log(value.DeptName + ' Selected'); 

      var DeptNameChanged = { 
       'Name': value 
      }; 

      DeptNameService.DeptNameValue(DeptNameChanged, function (res) { 

       $scope.FilterDetails = JSON.parse(res.data); 

       $scope.PaginationTrigger($scope.FilterDetails); //CODE APPLIED HERE 

      }); 


     }; 
    } 
})(); 

According to the above code the ERROR IS: angular.js:13642 TypeError: Cannot read property 'length' of undefined

は、どのように私はこれを達成することができますか?助けに感謝します。 details以来のおかげ

+0

[詳細] '$スコープを試みるが、あなたはどこでも$ scope.detailsを定義していない – Sravan

+0

を.length'、あなただけの詳細(ない$ scope.details、定期的な詳細)を受け –

答えて

0

私はついにそれを理解しました。これが私が達成しようとしたものであり、成功しました。何度も私を助けてくれる@Sravanに感謝します。また、お手伝いいただきありがとうございます。

コードは次のとおりです。学習のための共有の考え。

//Create a function in the controller 
function paginationTrigger(value) { 

    $scope.nums = ["10", "20", "30"]; 
    $scope.viewBy = $scope.nums[0]; 
    $scope.totalEmployees = value.length; 
    $scope.currentPage = 1; 
    $scope.itemsPerPage = $scope.viewBy; 
    $scope.maxSize = 5; 

    $scope.setPage = function (pageNo) { 
     $scope.currentPage = pageNo; 
    }; 

    $scope.pageChanged = function() { 
     console.log('Page changed to: ' + $scope.currentPage); 
    } 

    $scope.setEmployeesPerPage = function (num) { 
     $scope.itemsPerPage = num; 
     $scope.currentPage = 1; 
    } 
} 

/* Call the function in the desired palce. 
In this case inside my service function */ 

EmployeeService.GetEmpDetails(function (res) { 
    $scope.FilterDetails = JSON.parse(res.data); 
    //pagination code 
    paginationTrigger($scope.FilterDetails); //Passing the value 

}); 
0

test = {"a" : 1} 
 
details = "a" 
 
alert(test[details])

使用、$scope[details].lengthがあるパラメータです。

$scope.paginationTrigger =function(details){ //This method is used to control the pagination 
     $scope.nums = ["10", "20", "30"]; 
      $scope.viewBy = $scope.nums[0]; 
      $scope.totalEmployees = $scope[details].length; 
      $scope.currentPage = 1; 
      $scope.itemsPerPage = $scope.viewBy; 
      $scope.maxSize = 5; 

      $scope.setPage = function (pageNo) { 
       $scope.currentPage = pageNo; 
      }; 

      $scope.pageChanged = function() { 
       console.log('Page changed to: ' + $scope.currentPage); 
      } 

      $scope.setEmployeesPerPage = function (num) { 
       $scope.itemsPerPage = num; 
       $scope.currentPage = 1; 
      } 
    } 

Plsがコードスニペットを見ると、エラーが表示されます。

+0

あなたは'($スコープを送りました。 FilterDetails) 'を' paginationTrigger'にコピーしてください。 – Sravan

+0

あなたは '$ scope.details'をしようとしていますが、' .'表記を使って直接送信したパラメータを参照することはできません。 – Sravan

+0

@Sravan Nopeは機能しませんでした。これは関数なしで正常に動作します。内部で 'EmployeeService.GetEmpDetails' –

関連する問題