2017-11-04 129 views
0

我有以下代碼我想調用$ http,但它不工作其顯示錯誤,當我刪除$ http.get內容我的頁面工作正常,如何注入$ http內容爲檢索服務器端代碼

 (function() 
     { 
      'use strict'; 

      angular 
       .module('app.cnt') 
       .controller('cntController', cntController); 

      /** @ngInject */ 
      function cntController(Contacts, cntsService, $mdSidenav,$http, User, $location,$timeout, $document, $mdMedia) 
      { 

       var vm = this; 
       vm.brandid = $cookies.get('brandid'); 
       vm.$http.get("http://localhost:3010/api/cnts/mobileuser/cntlist/"+ vm.brandid).then(res => { 
        vm.contacts = res.data; 
       }).catch(err => { 
        console.log('asas'); 


        }); 
     } 
})(); 

消息: 錯誤解析: 「主\ CNT \ cnt.controller.js」,第20行:意外令牌=>

+0

您可以直接嘗試使用$的HTTP而不是VM $ HTTP –

+0

不工作:( – Sami

+0

請問您的瀏覽器不支持ES6箭頭功能 –

回答

-1

這是一個語法錯誤。用圓括號包裝函數參數。

(res) => {...} 
(err) => {...} 
+0

這是不正確的。單個參數箭頭函數不需要圓括號。 –

0

你嘗試使用的回報,因爲這是我怎麼做我的

function cntController(Contacts, cntsService, $mdSidenav, $http, User, $location, $timeout, $document, $mdMedia) { 
var vm = this; 
vm.brandid = $cookies.get('brandid'); 

function getContacts(){ 
    return $http.get("http://localhost:3010/api/cnts/mobileuser/cntlist/" + vm.brandid).then(res => { 
      vm.contacts = res.data; 
     }).catch(err => { 
      console.log('asas'); 
     }); 
}} 

,我建議你創建一個服務調用你的Http請求

1

加$注射= []作爲一個數組並傳遞相關服務。

就像那個:。

cntController.$inject = ['Contacts', 
'cntsService','$mdSidenav','$http', 'User', '$location','$timeout','$document', '$mdMedia'] ;