AngularJS-将配置文件添加到项目中

AngularJS - Adding Configuration File to Project

本文关键字:项目 添加 配置文件 AngularJS-      更新时间:2023-09-26

config.properties

foo="fooTest"
bar="barTest"

configGrabber.java

//Not going to paste all the code
//It successfully returns the JSON containing the config.properties file
//path to this is /resources

load-config.js

(function () {
  'use strict';
    angular.module('configService', [])
        .service('configFac', function ($http) {
            var data;
            $http.get('../api/resources').then(function (response) {
                data = response.data;
            });
        });
}());

app.js

(function () {
     'use strict';
  angular.module('app', ['configService'])
    .controller('appCtrl', function ($scope, configService, $q) {
        var data;
        $q.all([configService]).then(function (response) {
            data = response[0];
        });
        $scope.configResources = data;
   });
}());

index.html

...
<div>{{configResources.foo}}</div>
<div>{{configResources.bar}}</div>
...

所以我试图在这个项目中添加一个配置文件,这样用户就可以轻松地自定义它。JSON文件加载到config.js很好,但数据没有传输到控制器,你能在这个代码中找到任何错误或缺陷吗?

如果服务只是因为这个原因而存在,因此不需要任何其他功能,那么它可以看起来像这样:

.service('configFac', function ($http) {
    return $http.get('../api/resources')
        .then(function success(response) {
            console.log('Success: '+JSON.stringify(response));
            return response;
        }, function error(error) {
            console.log('Error: '+JSON.stringify(error));
            return error;
    });
};