我正在尝试使用Hello创建简单页面并使用PathVariable命名用户。好吧,如果你来http://localhost:8080/#/John,你会在网站上看到" Hello John"。
我知道Java很好,但在AngularJS我是初学者。我认为它的问题。
HelloController
@Controller
@RequestMapping("/{name}")
public class HelloController {
@RequestMapping(value = "/{name}", method = RequestMethod.GET)
public @ResponseBody
String name(@PathVariable String name) {
return this.name(name);
}
@RequestMapping("/layout")
public String getHomePage(ModelMap modelMap) {
return "hello/layout";
}
}
角度
中的HelloControllervar HelloController = function($scope, $http) {
var name = $name;
$scope.name = function(name) {
$http.get(name).success(function() {
})
};}
app.js
var AngularSpringApp = {};
var App = angular.module('AngularSpringApp', ['AngularSpringApp.filters', 'AngularSpringApp.services', 'AngularSpringApp.directives']);
// Declare app level module which depends on filters, and services
App.config(['$routeProvider', function ($routeProvider) {
$routeProvider.when('/home', {
templateUrl: 'home/layout',
controller: HomeController
});
$routeProvider.when('/:name', {
templateUrl: 'hello/layout',
controller: HelloController
});
}]);
简单页面hello / layout.html
<div class="alert alert-error" ng-show="error">{{errorMessage}}</div>
<form class="form-horizontal">
<h1>Hello {{name}}</h1>
</form>
感谢您的帮助!
答案 0 :(得分:0)
您可以在加载angularJS时从URL中提取名称,并将其与名称范围变量绑定。
angular.module('sortApp', [])
.controller('mainController', function($scope,$window) {
$scope.name=$window.location.href.split("#/")[1];
console.log($scope.name);
});
然后使用HTML中的{{name}}绑定它