$ http获取的动态网址 [英] Dynamic url for $http get

查看:91
本文介绍了$ http获取的动态网址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


$ b

  [{name)
来自urldatas的Json:

:John}]

JS文件:

  var app = angular.module('app',[]); 
app.service('service',function($ http,$ q){
this.getDatas = function(){
var datas = $ http.get('urldatas',{缓存:false});
返回$ q.all({datas});
};
app.controller('FirstCtrl',函数($ scope,service,$ http,$超时){
var vm = this;
vm.loadData = function(){
var promise = service.getDatas();
promise.then(function(data){
$ scope.datas = data.datas.data;
console.log($ scope.datas);
});
};
vm.loadPackages = function(){
var url =urlPackages+ datas.name;
$ http.get(url).then(function(response){
$ scope.MyPackages = response.data ;
});
};

所以我尝试动态更改网址在getPackages中的$ http.get中,通过getDatas的值,但我不知道如何去做在控制台中显示urlPackagesundefinded。提前给出答案。

解决方案

$ q 一个数组,而不是一个对象。删除大括号并添加一个方括号

$ $ $ $ $ $ $ $ $ var data = $ http.get('urldatas',{cache:false} );
返回$ q.all([datas]);

因为您引用了 controllerAs 变量,并通过 vm



然后承诺响应数据来自 data 属性

  var promise = service。 getDatas(); 
promise.then(function(response){
vm.datas = response.data.datas.data;
console.log(vm.datas);
});


Here is what i try to do : Json from "urldatas":

[{ "name" : "John" }]

JS file:

 var app = angular.module('app', []);
    app.service('service', function($http, $q){
        this.getDatas = function () {
            var datas = $http.get('urldatas', {cache: false});
            return $q.all({datas});
        };
    app.controller('FirstCtrl', function($scope, service, $http, $timeout) {
        var vm = this;
        vm.loadData = function () {
            var promise = service.getDatas();
            promise.then(function (data) {
                $scope.datas = data.datas.data;
                console.log($scope.datas);
            });
        };
    vm.loadPackages = function () {
            var url = "urlPackages" + datas.name;
            $http.get(url).then(function (response) {
            $scope.MyPackages = response.data;
        });
};

So i try to dynamicly change url in $http.get in getPackages, by values from getDatas, but i don't know how to do it. url in console shows "urlPackagesundefinded". Thanks for answers in advance.

解决方案

$q send multiple requests as an array, not as an object. remove the curly bracket and add a square bracket

var datas = $http.get('urldatas', {cache: false});
return $q.all([datas]);

since you reference the controllerAs remove the scope variables and reference them through vm.

also in then promises response data comes under data property

var promise = service.getDatas();
promise.then(function (response) {
   vm.datas = response.data.datas.data;
   console.log(vm.datas);
});

这篇关于$ http获取的动态网址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆