2

ng-init에서 함수를 호출하려고합니다. html 파일입니다. 이 함수는 API 호출을하고 데이터를 제공합니다. 그 데이터를 범위 변수에 할당하고 그 범위 변수를 지시문에 전달했습니다.Angularjs - ng-init 함수 완료 후 호출 지시문

컨트롤러가 먼저 타격입니다. 하지만 APIi 호출 완료 전에 지시문을 쳤어. 그래서 컨트롤러에 전달할 범위 변수는 정의되지 않았습니다.

App.directive('foldertree', function() { 
    return { 
     restrict: 'A', 
     scope: { 
      'inputfromapicall': '=', 
      'fileName': "=" 
     }, 
     link: function (scope, element, attrs, ngModelCtrl) { 

      return $timeout(function() {    
       $('#divid').fileTree({ 
        root: scope.inputfromapicall, //undefined 
        script: '/project/current/source/data/jqueryFileTree.jsp', 
        expandSpeed: 1, 
        collapseSpeed: 1, 
        multiFolder: false 
       }, function (file) { 
        scope.fileName = file;     
        scope.$apply(); 
       }); 
      }); 
     } 
    }; 
}); 

는 위 내 지시 코드 몇 가지 중 하나가 수정을 도와 막연한 question.Hope을 게시

죄송합니다.

+1

에서 누군가가 도움이되기를 바랍니다. 또한 ng-init은 API 호출을 수행하기에 좋은 곳이 아닙니다. 컨트롤러에서이 호출을하는 것이 좋습니다. –

답변

2

댓글 범위에 언급 된 MajoB가 $ watch done the trick. 여기에 업데이트 된 지시문 코드가 있습니다.

automateOnApp.directive('foldertree', ['$timeout', function($timeout){ 
    return { 
     restrict: 'A', 
     scope: { 
      'inputfromapicall': '=', 
      'fileName': "=" 
     }, 
     link: function (scope, element, attrs, ngModelCtrl, controller) { 
      scope.fileName = ''; 
      return $timeout(function() { 


       scope.$watch(function() {       
         return scope.inputfromapicall; 
       }, function(newVal) {      

         if(!angular.isUndefined(scope.inputfromapicall)){ 
          $('#divid').html(''); 
          $('#divid').fileTree({ 
            root: newVal, 
            script: '/project/current/source/data/jqueryFileTree.jsp', 
            expandSpeed: 1, 
            collapseSpeed: 1, 
            multiFolder: false 
          }, function (file) {       
            scope.fileName = file;     
            scope.$apply(); 
          }); 
         } 
       }); 

      }); 
     } 
    }; 
}]); 

은 당신이 $ 지시어의 모델 변경을 위해주의해야 할 미래

관련 문제