J
J
Jiakki-js2014-11-04 11:35:32
JavaScript
Jiakki-js, 2014-11-04 11:35:32

How to load controller before directive?

How to load directive before controller?

(function () {
var app = angular.module('app',["all-directive"]);
  
  app.controller('pustTextCtrl', function($scope, $http){
    $http.get("json/data.json").success(function(data){
      $scope.data = data;
    });
  }); 
}();

(function () {
  var app = angular.module('all-directive',[]);

  app.directive('anyDirective', function(){
    return {
      restrict: 'A',
      link: function($scope, elem, iAttrs, controller) {
        alert(elem.children());[] // пустой массив /а нужны данные
      }
    };
  });
})();

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
Sergey, 2014-11-04
Protko @Fesor

swap these two blocks.

B
Boris Benkovsky, 2014-11-04
@benbor

1. Use CODE tags.
It's much easier to read the source code this way... amazing

(function () {
  var app = angular.module('app',["all-directive"]);

  app.controller('pustTextCtrl', function($scope, $http){
    $http.get("json/data.json").success(function(data){
      $scope.data = data;
    });
  });
}();

(function () {
  var app = angular.module('all-directive',[]);

  app.directive('anyDirective', function(){
    return {
      restrict: 'A',
      link: function($scope, elem, iAttrs, controller) {
        //alert(elem.children());[] // пустой массив /а нужны данные
        $scope.$watch('data', function(newData){
           if(newData) {  // первый раз watch вызовется когда ajax данные еще не придут
              alert(newData); // пользуйтесь
           }
        });
      }
    };
  });
})();

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question