简体   繁体   English

Angularjs指令从控制器中的ajax调用中获取数据

[英]Angularjs directive to get data from ajax call in controller

I'm trying to use the jQuery fancy tree plugin with Angular. 我正在尝试使用带有Angular的jQuery花式树插件。 The source data for the tree comes from an ajax call in my controller. 树的源数据来自我的控制器中的ajax调用。 I'm then trying to get that data into my directive and load the tree. 我然后尝试将该数据导入我的指令并加载树。 Say the service takes 2 seconds to get the data. 假设该服务需要2秒才能获取数据。 Heres a cut down version of my code. 这是我的代码的缩减版本。

HTML HTML

<div tree-view ></div>

SERVICE 服务

angular.module('sc.services', []).
  factory('scService', function ($http) {

      var scApi = {};

      scApi.getsc = function () {
          return $http({
              url: config.Url.sc
          });
      };

      return scApi;
  });

CONTROLLER CONTROLLER

angular.module('sc.controllers', []).
  controller('scController', function ($scope, scService) {

      scService.getsc().success(function (response) {
          $scope.sc = response;
      });
  });

DIRECTIVE 指示

angular.module('sc.directives', [])
    .directive('treeView',function () {
        return {
            restrict: 'A',
            link: function (scope, element, attrs) {
                scope.$watch('sc', function() {
                    $(element).fancytree({
                        source: scope.sc
                    });
                });
            }
        }
    });

Jquery loads the fancy tree onto the page (so the directive is being called) but without an data. Jquery将奇特的树加载到页面上(因此调用了指令)但没有数据。 I also bind the data to a table on the page just to make sure its loading ok and that works. 我还将数据绑定到页面上的表,以确保其加载正常并且可行。 What am I missing? 我错过了什么?

Is this the correct way to do this? 这是正确的方法吗?

You could just let the directive wait till the data is retrieved. 你可以让指令等到检索数据。 Your watch might get executed once before the data is retrieved, so just check for the value in the watch to make sure it it has been retrieved already. 您的手表可能会在检索数据之前执行一次,因此只需检查手表中的值以确保它已被检索。 Something like this:- 像这样: -

        link: function (scope, element, attrs) {

           var unwatch =  scope.$watch('sc', function(v) {
               if(v){ // Check if you got the value already, you can be more specific like angular.isObject/angular.isArray(v)
                 unwatch(); //Remove the watch
                 $(element).fancytree({
                    source: v //or same as scope.sc
                 });
               }
            });

You could also use ng-if directive on the directive element, provided your directive is of lesser priority than ng-if. ng-if指令的优先级低于ng-if,也可以在指令元素上使用ng-if指令。 You r directive will not render until sc gets defined. sc定义之前,你的指令不会渲染。

  <div tree-view ng-if="sc"></div>

With 1.3.x you could even make use of 1 time binding. 使用1.3.x,您甚至可以使用1次绑定。

 <div tree-view ng-if="::sc"></div>

You don't need to watch the scope in the directive, so this should be the code for the directive's link method: 您不需要在指令中查看范围,因此这应该是指令的链接方法的代码:

link: function (scope, element, attrs) {
    $(element).fancytree({
         source: scope.sc
    });
}

You might also want to call the AJAX in the route's resolve, to have it ready when the controller starts. 您可能还想在路由的解析中调用AJAX,以便在控制器启动时准备好它。 Makes the code much cleaner. 使代码更清晰。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM