2013-07-17 1 views
1

파일의 텍스트를 rootscope 내부의 문자열에 저장하려고합니다.파일 판독기 오류를 통과하는 Angular.js 범위

# Text file 
Hello, world! 

출력 :

을 내가 지금까지이 제대로로드하는 지시어, 그리고 말한다 TEXTFILE에 대해 다음 OUPUT를 반환 루트

function callData($rootScope) { 
    console.log("function working"); 
    console.log($rootScope.data); 
} 

angular.module('spreadsheetApp').directive('fileReader', function($rootScope) { 
    return { 
     restrict: 'E', 
     scope: true, 
     templateUrl:'views/fileReaderTemplate.html', 
     controller: 'fileReaderController', 
     link: function (scope, element, attributes, controller) { 
      $element.bind("change", function(event) { 
       var files = event.target.files; 
       var reader = new FileReader(); 
       reader.onload = function() { 
        $rootScope.data = this.result; 
        console.log($rootScope.data); 
        callData($rootScope.data); 
        console.log("55"); 
       };     
       reader.readAsText(files[0]); 
      }); 
     } 
    }  
}); 

을 확인하는 기능입니다

Hello, world! 
function working 
fileDirective.js:44 
Uncaught TypeError: Cannot read property 'data' of undefined fileDirective.js:45 
callData fileDirective.js:45 
reader.onload 
당신은 함수 외부 범위를 액세스하기 위해 노력하고 있기 때문에

답변

1

,이 몇 가지 추가 작업을 필요로한다.

질문에 대한 대답은 이미 답이 여기있다 : 당신이 코드를 사용할 필요가 AngularJS access scope from outside js function

:

var scope = angular.element($("#yourelement")).scope(); 

CFR : http://jsfiddle.net/arunpjohny/sXkjc/8/

난 그냥 rootScope에 액세스 할 수있는 방법을
+0

, angular.element 필요없이? 당신이 함수에서 rootscope을 전달하려는 것처럼 – user1876508

+1

, 당신은 callData ($ rootScope)를 수행해야합니다; 대신 callData ($ rootScope.data)의 ; 당신이 지금 callData 기능에서 수행하려는 것은 실제로 $ 존재하지 않는 routScope.data.data, 따라서 오류 때문에 ... –