gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 13:09:22 24 4
gpt4 key购买 nike

配置.属性

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

加载配置.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 文件可以正常加载到 load-config.js,但是数据没有传输到 Controller ,您可以在这段代码中找到任何错误或缺陷吗?

最佳答案

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

.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;
});
};

关于javascript - AngularJS - 将配置文件添加到项目中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34134134/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com