2012-08-09 88 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)上创建一个手表并在访问前检查新值。