2014-01-14 4 views
0

Я пытаюсь следить за асинхронные данными в моей директиве, вот мой JS код:angularJS: смотреть асинхронные данные в директиве

(function(angular) { 
    var myApp = angular.module('testTree', []); 

    myApp.config(function($httpProvider) { 

     $httpProvider.defaults.headers.get = {}; 

     $httpProvider.defaults.headers.get["Content-Type"] = "application/json"; 

    }); 

    myApp.factory('DataService', function($http) { 

     return { getData: function(prmParentId, prmParentSrc) { 
      var data = $.param({ 'parentId': prmParentId, 'parentSrc': prmParentSrc }); 
      return $http.get("Temp.aspx/GetData", { params: { parentId: prmParentId, parentSrc: prmParentSrc }, data: '' }). 
      success(function(result) { 
       return result.d; 
      }); 
     } 
     } 
    }); 

    myApp.controller('myController', myController); 

    function myController($scope, DataService) { 
     $scope.treeNodes = []; 
     $scope.init = function() { 

      DataService.getData(0, 0).then(function(promise) { 
       $scope.treeNodes = promise.data.d; 
      }); 
     } 

     $scope.focusNode = function() { 
      console.log("kuku2"); 
     } 

    } 

    myApp.directive('nodes', function() { 
     return { 
      restrict: "E", 
      replace: true, 
      scope: { 
       nodes: '=', 
       clickFn: '&' 
      }, 
      template: "<ul><node ng-repeat='node in nodes' node='node' click-fn='clickFn()'></node></ul>", 
      link: function(scope, element, attrs) { 
       scope.$watch('treeNodes', function(newVal, oldVal) { 

        console.log(scope.treeNodes); 
       }, true); 
      } 

     } 
    }); 

    myApp.directive('node', function($compile) { 
     return { 
      restrict: "E", 
      replace: true, 
      scope: { 
       node: '=', 
       clickFn: '&' 
      }, 
      template: "<li><span ng-click='clickFn()(node)'>{{node.ObjectName}}</span></li>", 
      link: function(scope, element, attrs) { 

       if (angular.isArray(scope.node.Children)) { 

        element.append("<nodes nodes='node.Children' click-fn='clickFn()'></nodes>"); 

        $compile('<nodes nodes="node.Children" click-fn="clickFn()"></nodes>')(scope, function(cloned, scope) { 

         element.append(cloned); 
        }); 
       } 
      } 
     } 
    }); 
})(angular); 

И это мой HTML:

<div ng-app="testTree"> 
     <div ng-controller="myController"> 
      <div ng-init="init()"> 
       <nodes node="treeNodes" click-fn="focusNode"></nodes> 
      </div> 
     </div> 
    </div> 

Консоль в часах директивы всегда «не определена». Что я здесь делаю неправильно? спасибо.

ответ

0

Провод treeNodes как nodes в вашей директиве. Поэтому вам нужно посмотреть nodes.

scope.$watch('nodes', function(newVal, oldVal) { 

        console.log($scope.nodes); 
       }, true); 

<div ng-app="testTree"> 
     <div ng-controller="myController"> 
      <div ng-init="init()"> 
       <nodes nodes="treeNodes" click-fn="focusNode"></nodes> 
      </div> 
     </div> 
    </div> 
+0

Вы абсолютно правы. Благодарю. – ronen

Смежные вопросы