简体   繁体   English

AngularJS:从工厂返回配置

[英]AngularJS : Returning a config from factory

Let me start by saying that I'm a complete Angular newbie. 首先,我要说我是一个完整的Angular新手。

I am trying to set a pseudo constant in my Angular app to be able to get some API configuration details from an $http.get() . 我试图在Angular应用中设置一个伪常量,以便能够从$http.get()获取一些API配置详细信息。 I read that the proper way to do this is to create a factory that returns the promise ( source ) and then inject that factory into the controller(s). 我读到,执行此操作的正确方法是创建一个返回诺言( source )的工厂,然后将该工厂注入到控制器中。

I believe I have done that properly, however I'm still getting the following error (note that when I remove the references to apiConfig in the controller, the errors disappear): 我相信我已经正确地做到了,但是仍然出现以下错误(请注意,当我在控制器中删除对apiConfig的引用时,错误会消失):

 Error: [ng:areq] http://errors.angularjs.org/1.3.10/ng/areq?p0=fn&p1=not%20aNaNunction%2C%20got%string
    at Error (native)
    at https://ajax.googleapis.com/ajax/libs/angularjs/1.3.10/angular.min.js:6:417
    at Qb (https://ajax.googleapis.com/ajax/libs/angularjs/1.3.10/angular.min.js:19:417)
    at sb (https://ajax.googleapis.com/ajax/libs/angularjs/1.3.10/angular.min.js:20:1)
    at Vb (https://ajax.googleapis.com/ajax/libs/angularjs/1.3.10/angular.min.js:34:283)
    at Object.e [as invoke] (https://ajax.googleapis.com/ajax/libs/angularjs/1.3.10/angular.min.js:36:439)
    at Object.$get (https://ajax.googleapis.com/ajax/libs/angularjs/1.3.10/angular.min.js:35:71)
    at Object.e [as invoke] (https://ajax.googleapis.com/ajax/libs/angularjs/1.3.10/angular.min.js:37:96)
    at https://ajax.googleapis.com/ajax/libs/angularjs/1.3.10/angular.min.js:38:410
    at d (https://ajax.googleapis.com/ajax/libs/angularjs/1.3.10/angular.min.js:36:308)

Here is my code: 这是我的代码:

Factory

app.factory('apiConfig', ['$http', 'API_KEY'], function ($http, API_KEY) {
    return $http.get('http://api.themoviedb.org/3/configuration?api_key=' + API_KEY);
});

Controller 调节器

appControllers.controller("MovieDetailCtrl", ['$scope', '$routeParams', '$http', 'API_KEY', 'apiConfig', function ($scope, $routeParams, $http, api_key, apiConfig) {
    "use strict";

    apiConfig.then(function (response) {
        $scope.apiConfig = response.data;
    });

    var setMovieObject = function (response) {
        $scope.movie = response.data;
    };

    $http.get('http://api.themoviedb.org/3/movie/' + $routeParams.movieId + '?api_key=' + api_key).then(setMovieObject);

}]);

I've also tried having the factory return an object, but that also didn't work. 我也尝试过让工厂返回一个对象,但这也没有用。 Here is the code I tried for that: 这是我为此尝试的代码:

Factory

app.factory('apiConfig', ['$http', 'API_KEY'], function ($http, API_KEY) {

    var theConfig = {};

    theConfig.getConfig = function () {
        return $http.get('http://api.themoviedb.org/3/configuration?api_key=' + API_KEY);
    };

    return theConfig;
});

Controller 调节器

appControllers.controller("MovieDetailCtrl", ['$scope', '$routeParams', '$http', 'API_KEY', 'apiConfig', function ($scope, $routeParams, $http, api_key, apiConfig) {
    "use strict";

    apiConfig.getConfig().then(function (response) {
        $scope.apiConfig = response.data;
    });

    var setMovieObject = function (response) {
        $scope.movie = response.data;
    };

    $http.get('http://api.themoviedb.org/3/movie/' + $routeParams.movieId + '?api_key=' + api_key).then(setMovieObject);

}]);

Also, as a follow up, is there any resources on reading AngularJS stack traces? 另外,作为后续,是否有阅读AngularJS堆栈跟踪的资源? Or is there a better way to debug AngularJS apps? 还是有更好的方法调试AngularJS应用? The errors I'm seeing in the console don't really make much sense. 我在控制台中看到的错误并没有多大意义。

For Debugging, please use Batarang . 要进行调试,请使用Batarang It is THE way to debug AngularJS applications :) 这是调试AngularJS应用程序的方法:)

Here are a few pointers: 这里有一些提示:

  • Use .then on $q type Promises only when you wish to further transform the value. 仅当您希望进一步转换值时,才对$ q类型使用.then承诺。
  • Use .failure and .success on $http-Promises. 在$ http-Promises上使用.failure和.success。 Both have the same signature: (data, httpStatus); 两者具有相同的签名:(数据,httpStatus); It may be possible that the error you are getting is due to some error which may not be client-sided (your access to the api may be denied, for that matter). 您遇到的错误可能是由于某些错误(可能不是客户端错误)引起的(就此而言,您对api的访问可能会被拒绝)。

Check for these first and use Batarang. 首先检查这些,然后使用Batarang。 Also: What kind of errors are you getting? 另外:您遇到什么样的错误? Where does the AngularJS Stacktrace point you to? AngularJS Stacktrace将您指向何处?

Found the problem. 找到了问题。 It was an incorrectly placed square bracket. 这是一个放置不正确的方括号。 Originally I had: 最初我有:

Factory(broken) 厂(碎)

app.factory('apiConfig', ['$http', 'API_KEY'], function ($http, API_KEY) {
    return $http.get('http://api.themoviedb.org/3/configuration?api_key=' + API_KEY;
});

The problem was I prematurely closed the array before passing Angular the actual factory function. 问题是我在将实际工厂函数传递给Angular之前过早关闭了数组。 The first/third lines of code should have been as follows: 代码的第一/第三行应如下所示:

Factory(fixed) 厂(固定)

app.factory('apiConfig', ['$http', 'API_KEY', function ($http, API_KEY) {
    return $http.get('http://api.themoviedb.org/3/configuration?api_key=' + API_KEY;
}]);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM