angularjs $ http不工作在我的代码请检查

angularjs $ http不工作在我的代码请检查

问题描述:

我有以下代码我想调用$ 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箭头功能 –

这是一个语法错误。用圆括号包装函数参数。

(res) => {...} 
(err) => {...} 
+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请求

加$注射= []作为一个数组并传递相关服务。

就像那个:。

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