路线不适用于angularJS 1.6

时间:2018-01-15 14:47:30

标签: javascript angularjs routing

我使用angularjs 1.6来创建一个简单的应用程序,只是为了显示一个组列表并显示每个组的所有用户...... 我的问题是我无法使用ng-view

显示用户列表

这是我的index.html:



<!DOCTYPE html>
<html lang="en" ng-app="application"> 
<head>
  <meta charset="utf-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <title>My application</title>
  <meta name="description" content="">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="stylesheet" type="text/css" href="assets/bootstrap-3/css/bootstrap.min.css">
  <link rel="stylesheet" href="assets/app.css">
</head>
<body>
  <div ng-controller="groupctrl">
    <div class="well">
      <ul ng-repeat="grp in group">
        <li class="link" ng-click="displayusers(grp.uuid)">{{ grp.name }}</li>
        <li class="link" ng-click="displayusers(grp.uuid)">{{ grp.name }}</li>
     </ul>
    </div>
  </div>
    <div ng-view>

    </div>
</body>
  <script src="assets/angular.min.js"></script>
  <script src="assets/angular-route.min.js"></script>
  <script src="controllers/app.js"></script>
  <script src="controllers/route.js"></script>
  <script src="controllers/service.js"></script>
  <script src="controllers/mainctrl.js"></script>

</html>
&#13;
&#13;
&#13;

app.js

&#13;
&#13;
var app = angular.module('application', ['ngRoute']);
&#13;
&#13;
&#13;

route.js和mainctrl.js

&#13;
&#13;
app.config(['$locationProvider', '$routeProvider', 
	function($locationProvider, $routeProvider) {

		  $locationProvider.hashPrefix('!');

		  $routeProvider
		  .when('/', {
	    	templateUrl: 'index.html'
		   })
		  // .when('/group', {
		  //   	templateUrl: 'group.html',
		  //   	controller: 'groupctrl'
		  //  })
		   .when('/users', {
		    	templateUrl: 'users.html',
		    	controller: 'usersctrl'
		   })
		  .otherwise({redirectTo: '/'});
}]);

app
.controller('groupctrl', function($scope, $timeout, $location, apiService) {

$scope.hello = "hello 123";

apiService.getgroup().then(function(data){
	$scope.group = data;
});

$scope.displayusers = function(idgroup){
	apiService.getusers(idgroup);
	$location.path('/users');
};

})
.controller('usersctrl', function($scope, $timeout, apiService) {


apiService.getusers().then(function(data){
	$scope.users = data;
});

});
&#13;
&#13;
&#13;

users.html

&#13;
&#13;
<div ng-controller="usersctrl">
    <div ng-repeat="user in users">
      <div class="well text-center">
         <h2>{{ user.name }}</h2>
         <h5>{{ user.index }}</h5>
             <button class="btn btn-success" ng-click="edit(user.index)">EDIT</button>
             <button class="btn btn-danger" ng-click="delete(user.index)">DELETE</button>
      </div>
    </div>
  </div> 
&#13;
&#13;
&#13;

感谢您的帮助!

1 个答案:

答案 0 :(得分:0)

参考问题作者提供的评论,问题在于错误提示访问users.html文件:

XMLHttpRequest cannot load file:///home/folder-test/Documents/application/users.html. Cross origin requests are only supported for protocol schemes: http, data, chrome, chrome-extension, https

此问题已在其他SO问题中解决,例如this,更具体地说是针对AngularJS here