我可以成功地从 /api/session.json 调用中获取数据并将其记录到我的控制台。
如何将此 api 调用的输出发送到我的 index.html 文件并替换“用户名”?
谢谢!
这是我的 Angular 应用程序的 app.js 文件:
'use strict';
var staticApp = angular.module('staticApp', [])
.config(['$routeProvider', function($routeProvider) {
$routeProvider
.when('/', {
templateUrl: 'views/main.html',
controller: 'MainCtrl'
})
.otherwise({
redirectTo: '/'
});
}])
.run( function($http) {
$http.defaults.useXDomain = true;
// API CALL
$http({method: 'GET', url: '/api/session.json'}).
success(function(data, status, headers, config) {
// THIS WORKS!!
// MY CONSOLE SHOWS THIS:
// Object {user: "devuser"}
console.log(data);
}).
error(function(data, status, headers, config) {
console.log(status);
});
});
这是我的 index.html 文件:
<!DOCTYPE html>
<head>
<!-- meta and css stuff -->
</head>
<body ng-app="staticApp">
<div class="navbar navbar-inverse navbar-fixed-top">
<div class="navbar-inner">
<div class="container-fluid">
<button type="button"
class="btn btn-navbar"
data-toggle="collapse" data-target=".nav-collapse">
</button>
<a class="brand" href="/">MyApp</a>
<div class="nav-collapse collapse">
<p class="navbar-text pull-right">
<!-- I WANT Username TO BE THE VALUE RETURNED FROM THE API CALL -->
Logged in as <a href="#" class="navbar-link">Username</a>
</p>
</div><!--/.nav-collapse -->
</div>
</div>
</div>
<div class="container" ng-view></div>
<script src="scripts/vendor/angular.js"></script>
<script src="scripts/app.js"></script>
<script src="scripts/controllers/main.js"></script>
</body>
</html>