【发布时间】:2014-08-08 01:42:45
【问题描述】:
我是 AngularJS 的新手,只是构建一个应用程序来学习它。我的应用程序调用 REST API,现在我在应用程序中硬编码了主机名。我想在应用程序设置中进行此设置(也许以后有地方可以配置它)。我以为我会从一个常数开始。它应该像这样进入我的 app.js 吗?如果是这样,我不确定将其添加到带有 $routeProvider 的 .config 设置的语法
(function () {
// Define module and add dependencies inside []
var app = angular.module("haClient", ["ngRoute"]);
app.constant('hostname', 'http://192.192.192.192:8176');
app.config(function ($routeProvider) {
$routeProvider
// Register routes
// Main route
.when("/main", {
templateUrl: "main.html",
controller: "MainController"//,
//activeTab: 'home'
})
// Device List
.when("/devices", {
templateUrl: "devicelist.html",
controller: "DeviceListController"
})
// Device details (param is device name)
.when("/device/:devicename", {
templateUrl: "device.html",
controller: "DeviceController"
})
// Invalid URL's get sent back to main route
.otherwise({ redirectTo: "/main" });
}); // End App Config
}());
这是需要使用它的模块(从控制器调用):
(function () {
var deviceCtrl = function ($http) {
var getDevices = function () {
return $http.get("http://192.192.192.192:8176/devices.json/")
.then(function (response) {
return response.data;
});
};
// get details and return a promise
var getDeviceDetails = function (deviceName) {
return $http.get("http://192.192.192.192:8176/devices/" + deviceName + ".json/")
.then(function (response) {
return response.data;
});
};
// Public API
return {
getDeviceDetails: getDeviceDetails,
getDevices: getDevices
};
};
var module = angular.module("haClient");
}());
有人可以启发他们设置和获取它的最佳方式吗?
谢谢
【问题讨论】: