2012-08-09 48 views
0

我正在爲我的結果做分頁指令,我似乎無法克服這個問題。我有以下代碼(CoffeeScript的):angularjs從作用域對象獲取值

window.pagination_services = angular.module("pagination_services", []) 
    .directive('paginate', -> 
     { 
      restrict: 'E', 
      replace: true, 
      templateUrl: '../../assets/app/partials/services/pagination.html', 
      scope: true, 
      link: ($scope, $element, $attrs) -> 
       console.log $scope.results 
     } 
    ) 

正如你可以看到我有一個$scope.results其中的console.log返回:

e 
    pagination: Object 
     current_page: 1 
     per_page: 20 
     total_entries: 4097 
     __proto__: Object 
    sales: Array[20] 
    __proto__: e 

我需要能夠訪問分頁的價值目的。例如:

$scope.results.pagination.current_page 

但是,無論我嘗試什麼,我都會得到未定義的內容。

任何想法?

回答

1

它看起來像你的指令取決於一個包含「結果」的控制器,並且你想把它作爲依賴注入到這個指令中。

說出控制器被稱爲ResultsController

你可能想要做這樣的事情

.directive('paginate', -> 
     { 
      restrict: 'E', 
      replace: true, 
      require : '^ResultsController' 
      templateUrl: '../../assets/app/partials/services/pagination.html', 
      scope: true, 
      link: ($scope, $element, $attrs) -> 
       console.log $scope.results 
     } 

希望這有助於!

+0

沒有。控制器是該指令的父級,所以我繼承它的屬性。我會發布解決方案。這完全是關於異步的。我的對象是一個httppromise,這就是爲什麼我無法獲取它。不能回答我自己的問題 – 2012-08-09 14:26:15

+0

你可以回答你自己的問題。 – ganaraj 2012-08-09 14:51:34

0

您可能得出同樣的結論......但您應該在結果(see scope doc)上創建一個手錶並在訪問前檢查新值。