angular-ui / ui-ace

This directive allows you to add ACE editor elements.
http://angular-ui.github.io/ui-ace
MIT License
578 stars 172 forks source link

Initializing ace editor content using $http service #177

Open prashant-pokhriyal opened 7 years ago

prashant-pokhriyal commented 7 years ago

I'm rendering content of the editor using $http service. But onLoad function is being called much before data is rendered in editor.

When I'm adding code inside $timeout service then it is working properly. Is there any way to achieve this without using $timeout service.

funciton onLoad(editor) {
   /*
     It will set the some of  the lines readyonly. But because onLoad is executing much 
     before data is rendered from $http service, it does not find any lines.
   */
   set_readonly_lines(editor);
}
funciton onLoad(editor) {
   /*
     Using timeout it is working properly
   */
   $timeout(function() {
      set_readonly_lines(editor);
   }, 1);
}
augustoeliezer commented 6 years ago

Store the editor from onLoad in $scope.

$scope.aceLoaded = function($editor) {

    $scope.$editor = $editor;
};

$scope.onOpen = function(code, fileExtension) {
    let mode = resolveExtension(fileExtension);
    $scope.html = code;
    $scope.$editor.session.setMode('ace/mode/' + mode);
}
prashant-pokhriyal commented 6 years ago

Excuse me for my poor inferring, but I'm not able to get you

augustoeliezer commented 6 years ago

Sorry I forgot add the service

$scope.openFile = function(file) {
    //Here I prefer use factory.
    //fileFactory.openFile(file).then(fn,fn);
    $http.post('/api', file).then(function (res) {
        $scope.onOpen( res.data.content, res.data.extension)
    },angular.noop);
}