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!

4
  • 3
    you are missing controller as syntax in your plunker ng-controller="FirstCtrl as vm" Commented Aug 17, 2017 at 7:29
  • 1
    also u are using logget twice; one for function and another for variable. Please refer my answer Commented Aug 17, 2017 at 7: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 Commented Aug 17, 2017 at 7: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 Commented Aug 17, 2017 at 7:45

3 Answers 3

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

Sign up to request clarification or add additional context in comments.

2 Comments

you are reassigning $scope.logget in $http response again.
fixed it @Rahul.
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');
            });
      }

});

Comments

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?

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.