0

I need to get a text from file text.log, Here's my code :

  vm.logget = function(){
        $http({
            method: 'Get',
            url: 'text.log'
        })
            .then(function successCallback(response) {
                $scope.logget = response;
                console.log($scope.logget);

            }, function errorCallback(response) {
                console.log(response);
                console.log('error');
            });

    }

text.log :

creating: /asd/ds/das/das-dsss/
creating: /asd/ds/das/das-dsss/
creating: /asd/ds/das/das-dsss/
creating: /asd/ds/das/das-dsss/
creating: /asd/ds/das/das-dsss/

PLunker : http://plnkr.co/edit/g1t4pludTTIAJYKTToCK?p=preview

I don't get anything in succesCallback. Thanks for answers in advance!

bafix2203
  • 541
  • 7
  • 25
  • 3
    you are missing controller as syntax in your plunker `ng-controller="FirstCtrl as vm"` – user1608841 Aug 17 '17 at 07:29
  • 1
    also u are using `logget` twice; one for function and another for variable. Please refer my answer – user1608841 Aug 17 '17 at 07:34
  • 1
    Using logget twice will work since one is defined on vm and another on $scope. But still it is better to use different varaibles in order to avoid any future discrepancy@Rahul – Vivz Aug 17 '17 at 07:38
  • 1
    @vivz absolutely correct; however if you are following controller as syntax then its better to not mess up with `$scope` unless its actually needed – user1608841 Aug 17 '17 at 07:45

3 Answers3

1

use ng-controller="FirstCtrl as vm" or change the function to

$scope.logget = function() {
   $http({
       method: 'Get',
       url: 'text.log'
     })
     .then(function successCallback(response) {
       $scope.data = response;
       console.log($scope.data);

     }, function errorCallback(response) {
       console.log(response);
       console.log('error');
     });
}

Working plunker

Sibiraj
  • 4,486
  • 7
  • 33
  • 57
1

Please try following code in your plunker: in HTML :

 <body ng-controller="FirstCtrl as vm">
      <button ng-click="vm.logget();" type="button" class="btn btn-default">Log</button>
          {{vm.loggetData}} <!--See I have changed the Binding variable-->
 </body>

and in Controller :

var app = angular.module('app', ['ui.bootstrap']);
app.controller('FirstCtrl', function($scope, $http) {
     var vm = this;
     vm.logget = function(){
        $http({
            method: 'Get',
            url: 'text.log'
        }).then(function(response) {
               vm.loggetData = response.data;
             }, function(response) {
                console.log(response);
                console.log('error');
            });
      }

});
user1608841
  • 2,455
  • 1
  • 27
  • 40
1

You have two problems in your code

First you have to use controller as syntax

ng-controller="FirstCtrl as vm"

Second you have to access the data property of your response

 vm.logget1 = response.data;

Working Plunker: http://plnkr.co/edit/cqhzeb4dJoyr7BSP2LAA?p=preview

Note: It is better if you use a different variable for storing your response since logget is already your function name. And also try to avoid $scope if you are using controller as syntax

For more Info on controller as: AngularJs "controller as" syntax - clarification?

Vivz
  • 6,625
  • 2
  • 17
  • 33