【问题标题】:Angular partials not working角部分不起作用
【发布时间】:2016-12-19 21:56:44
【问题描述】:

我以前从未遇到过这个问题,我知道这是一个小问题......但我似乎无法弄清楚出了什么问题......我的部分部分没有正确加载,而是撞到了其他路线回到我的 /login (我已分配)。 每次我单击创建调查链接时,它都会将我重定向回登录名,并且 URL 会显示此 http://localhost:3000/#!/login#%2Fcreate,但是当我将 http://localhost:3000/#!/create 放入 URL 时,它会起作用。不太确定是什么问题。请帮忙!

var app = angular.module('myApp', ['ngRoute']);

app.config(function($routeProvider){
	$routeProvider
	.when('/login', {
		templateUrl: 'partials/login.html',
		controller: 'UserController'
	})
	.when('/dashboard', {
		templateUrl: 'partials/dashboard.html',
		controller: 'SurveyController'
	})
	.when('/create', {
		templateUrl: 'partials/create.html',
		controller: 'SurveyController'
	})
	.when('/poll/:id', {
		templateUrl: 'partials/poll.html'
	})
	.otherwise({
		redirectTo: '/login'
	});
})
<!DOCTYPE html>
<html>
<head>
	<title>Survey Polls</title>
	<!-- CSS -->
	<link rel="stylesheet" href="bootstrap/dist/css/bootstrap.css">
    <link rel="stylesheet" type="text/css" href="styles.css">	
	<!-- Angular -->
	<script src="angular/angular.js"></script>
	<script src="angular-route/angular-route.js"></script>
	<!-- jQuery/Bootstrap -->
	<script src="jquery/dist/jquery.js"></script>
	<script src="bootstrap/dist/js/bootstrap.js"></script>
	<!-- App.js -->
	<script src="app.js"></script>
	<!-- Controllers/Factories -->
	<script src="controllers/SurveyController.js"></script>	
	<script src="controllers/UserController.js"></script>
	<script src="factories/SurveyFactory.js"></script>
	<script src="factories/UserFactory.js"></script>
</head>
<body ng-app="myApp">
	<div class="container">
		<div ng-view></div>
	</div>
</body>
</html>




<a href="#/create">Create a new survey</a>

<h3>Current polls:</h3>

<table border="1">
	<thead>
		<tr>
			<th>Name</th>
			<th>Survey Question</th>
			<th>Date Posted</th>
			<th>Action</th>
		</tr>
	</thead>
	<tbody>
		<tr ng-repeat="s in surveys">
			<td>{{ s._user.username }}</td>
			<td>{{ s.question }}</td>
			<td>{{ s.created_at }}</td>
			<td><button ng-show="loggedInUser._id == s._user._id" ng-click="delete(s._id)">Delete</button></td>
		</tr>
	</tbody>
</table>

【问题讨论】:

  • 哪个不工作创建?
  • 是的,抱歉,我为此编辑了我的帖子。哎呀!
  • 向我们展示您如何导航以创建状态...触发它的函数。
  • @Thalaivar 当我点击“创建新调查”链接时
  • 你能创建一个 plunker 吗?我已经尝试了一半的代码,它似乎可以工作,顺便说一下,是否有意让 SurveyController 用于创建和仪表板?

标签: angularjs partials


【解决方案1】:

我找到了一个可能的解决方案here。将此行添加到您的配置中以消除感叹号(!):

$locationProvider.hashPrefix('');

【讨论】:

  • 非常感谢! :)
  • 很高兴有帮助。
【解决方案2】:

尝试使用 ng-href 而不仅仅是 href。

【讨论】:

猜你喜欢
  • 2018-05-31
  • 2013-12-09
  • 2016-05-08
  • 1970-01-01
  • 1970-01-01
  • 2020-06-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多