2017-08-17 118 views
0

我需要從文件text.log文本,這裏是我的代碼:

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

我不在succesCallback中獲取任何東西。 感謝您提前給出答案!

+3

你缺少控制器在plunker'NG-控制器=「FirstCtrl爲VM」語法' – Rahul

+1

你也使用'logget'兩次;一個用於功能,另一個用於變量。請參閱我的回答 – Rahul

+1

使用logget兩次將工作,因爲一個是在vm上定義的,另一個是在$ scope上定義的。但仍然最好使用不同的變量,以避免任何未來的差異@ Rahul – Vivz

回答

1

使用ng-controller="FirstCtrl as vm"或改變功能

$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'); 
    }); 
} 

工作plunker

+0

你再次在'$ http'響應中重新分配'$ scope.logget'。 – Rahul

+0

修復了@Rahul。 –

1

請儘量按照你的plunker代碼: 在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> 

,並在控制器:

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'); 
      }); 
     } 

}); 
1

你在你的代碼

前兩個問題,你必須使用控制器作爲語法

ng-controller="FirstCtrl as vm"

其次,你必須訪問響應的數據屬性

vm.logget1 = response.data; 

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

注意:因爲logget已經是您的函數名稱,所以最好使用不同的變量來存儲您的響應。而且也儘量避免$scope如果您正在使用控制器作爲語法

有關控制器的更多相關資料爲AngularJs "controller as" syntax - clarification?