$ routeProvider,注入angularjs,无法识别错误

时间:2016-05-27 11:24:46

标签: angularjs ngroute

点击注册后,只需更改网址,不会将其移至register.html或显示内容

Screenshot

html文件

<!DOCTYPE html>
<html>
 <head>
<title></title>
<script src = "http://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js"></script>
<script src = "http://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular-route.min.js"></script>
<script src="Script/Scc.js"></script>
</head>
<body ng-app="sampleApp">
First Name<input type="text" ng-model="reg.firstName" /><br />
Last Name<input type="text" ng-model="reg.lastName" />  <br />          
<a href="#Register"> Register </a> 
 </body>

Scc.js

var sampleApp = angular.module('sampleApp', ['ngRoute']);
sampleApp.config(['$routeProvider',
function ($routeProvider) {
  $routeProvider.
    when('/#Register', {
        templateUrl: '/register.html',
        controller: 'registerController'
    })
  }]);
sampleApp.controller('registerController', function ($scope,$route) {
var reg = {
    firstName: 'Mmm',
    lastName: 'Nnn',
};
$scope.reg = reg;
});

register.html

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
</head>
<body>
<h2>Registration</h2>
First Name is{{ reg.firstName }}<br />
Last Name is   {{reg.lastName}}
</body>
</html>

无法检索文本框内容并在register.html中显示...

1 个答案:

答案 0 :(得分:0)

你的html中需要ng-view指令来指示应该注入模板的位置。类似于<div ng-view></div>

并且您的register.html不应该是完整的HTML文档,它应该是仅包含需要注入的数据的部分模板。

你不需要路线#

when('/Register', {
    templateUrl: '/register.html',
    controller: 'registerController'
});

确保模板的路径正确。