将异步获得的数据传递给指令



我目前有一个AngularJS controller,它基本上是通过$http.get()调用异步获得一些JSON,然后将获得的数据链接到一些范围变量。

controller代码的恢复版本:

mapsControllers.controller('interactionsController', ['$http', function($http) {
  var ctrlModel = this;
  $http.get("data/interactionsPages.json").
  success(function(data) {
    ctrlModel.sidebar = {};
    ctrlModel.sidebar.pages = data;
  }).
  error(function() {...});
}]);

然后,我有一个自定义的directive,它通过HTML元素接收相同的范围变量。

directive代码的恢复版本:

mapsDirectives.directive('sidebar', function() {
  return {
    restrict : 'E',
    scope : {
      pages : '@'
    },
    controller : function($scope) {            
      $scope.firstPage = 0;
      $scope.lastPage = $scope.pages.length - 1;
      $scope.activePage = 0;
      //...
    },
    link : function(scope) {
      console.log(scope.pages);
    },
    templateURL : 'sidebar.html'
  }
});

HTML:的恢复版本

<body>
  <div ng-controller='interactionsController as interactionsCtrl'>
    <mm-sidebar pages='{{interactionsCtrl.ctrlModel.sidebar.pages}}'>
    </mm-sidebar>
  </div>
</body>

问题是,由于$http.get()是异步的,指令初始化不正确(例如:$scope.pages.length - 1未定义)。

我找不到任何能解决这个问题的东西,尽管有一些解决方案似乎可以解决这个问题。也就是说,我试图观察变量,只在检测到变化后初始化变量,正如许多其他帖子中所建议的那样。为了进行测试,我使用了类似的东西:

//... inside the directive's return{ }
link: function() {
  scope.$watch('pages', function(pages){
    if(pages)
      console.log(pages);
  });
}

我已经测试过了,$watch函数没有被调用过一次(记录的值是undefined),我认为这意味着它没有检测到变量值的变化。但是,我确认该值正在更改。

那么,这里的问题是什么?

移动控制器中sidebar对象的声明,并将作用域绑定更改为=

mapsDirectives.controller("interactionsController", ["$http", "$timeout",
    function($http, $timeout) {
        var ctrlModel = this;
        ctrlModel.sidebar = {
            pages: []
        };
      /*
      $http.get("data/interactionsPages.json").
          success(function(data) {
          //ctrlModel.sidebar = {};
          ctrlModel.sidebar.pages = data;
       }).
       error(function() {});
      */
      $timeout(function() {
        //ctrlModel.sidebar = {};
        ctrlModel.sidebar.pages = ["one", "two"];
      }, 2000);
    }
]);
mapsDirectives.directive('mmSidebar', [function() {
    return {
      restrict: 'E',
      scope: {
        pages: '='
      },
      controller: function() {},
      link: function(scope, element, attrs, ctrl) {
        scope.$watch("pages", function(val) {
          scope.firstPage = 0;
          scope.lastPage = scope.pages.length - 1;
          scope.activePage = 0;
        });
      },
      templateUrl: 'sidebar.html'
    };
}]);

然后匹配指令名称并去掉大括号。

<mm-sidebar pages='interactionsCtrl.sidebar.pages'>
</mm-sidebar>

下面是一个工作示例:http://plnkr.co/edit/VP79w4vL5xiifEWqAUGI

问题似乎是您的html标记。

在控制器中,您已指定ctrlModel等于this

在html标记中,您已经将相同的this声明为interactionsController
因此,将ctrlModel附加到interactionsController是不正确的。

<body>
  <div ng-controller='interactionsController as interactionsCtrl'>
    <!-- remove this -->
    <mm-sidebar pages='{{interactionsCtrl.ctrlModel.sidebar.pages}}'>
    <!-- replace with this -->
    <mm-sidebar pages='{{interactionsCtrl.sidebar.pages}}'>
    </mm-sidebar>
  </div>
</body>

最新更新