The*_*ink 237 javascript angularjs ngroute
我正在尝试使用AngularJS 的ng-click功能来切换视图.我将如何使用下面的代码执行此操作?
的index.html
<div ng-controller="Cntrl">
<div ng-click="someFunction()">
click me
<div>
<div>
Run Code Online (Sandbox Code Playgroud)
controller.js
function Cntrl ($scope) {
$scope.someFunction = function(){
//code to change view?
}
}
Run Code Online (Sandbox Code Playgroud)
gan*_*raj 314
为了在不同的视图之间切换,您可以直接在index.html文件中更改window.location(使用$ location服务!)
<div ng-controller="Cntrl">
<div ng-click="changeView('edit')">
edit
</div>
<div ng-click="changeView('preview')">
preview
</div>
</div>
Run Code Online (Sandbox Code Playgroud)
Controller.js
function Cntrl ($scope,$location) {
$scope.changeView = function(view){
$location.path(view); // path not hash
}
}
Run Code Online (Sandbox Code Playgroud)
并配置路由器根据位置切换到不同的部分(如https://github.com/angular/angular-seed/blob/master/app/app.js所示).这将有历史的好处以及使用ng-view.
或者,你使用ng-include和不同的部分,然后使用ng-switch,如下所示(https://github.com/ganarajpr/Angular-UI-Components/blob/master/index.html)
PW *_*Kad 38
提供的答案是绝对正确的,但我希望扩展可能想要更动态地做任何未来的访问者 -
在视图中 -
<div ng-repeat="person in persons">
<div ng-click="changeView(person)">
Go to edit
<div>
<div>
Run Code Online (Sandbox Code Playgroud)
在控制器中 -
$scope.changeView = function(person){
var earl = '/editperson/' + person.id;
$location.path(earl);
}
Run Code Online (Sandbox Code Playgroud)
与接受的答案相同的基本概念,只是添加一些动态内容以改善一点.如果接受的答案想要添加这个,我将删除我的答案.
Cod*_*ody 23
我有一个工作的例子.
这是我的文档的样子:
<html>
<head>
<link rel="stylesheet" href="css/main.css">
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.0.3/angular.min.js"></script>
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.0.3/angular-resource.min.js"></script>
<script src="js/app.js"></script>
<script src="controllers/ctrls.js"></script>
</head>
<body ng-app="app">
<div id="contnr">
<ng-view></ng-view>
</div>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
这是我的部分看起来像:
<div id="welcome" ng-controller="Index">
<b>Welcome! Please Login!</b>
<form ng-submit="auth()">
<input class="input login username" type="text" placeholder="username" /><br>
<input class="input login password" type="password" placeholder="password" /><br>
<input class="input login submit" type="submit" placeholder="login!" />
</form>
</div>
Run Code Online (Sandbox Code Playgroud)
这是我的Ctrl看起来像:
app.controller('Index', function($scope, $routeParams, $location){
$scope.auth = function(){
$location.url('/map');
};
});
Run Code Online (Sandbox Code Playgroud)
app是我的模块:
var app = angular.module('app', ['ngResource']).config(function($routeProvider)...
Run Code Online (Sandbox Code Playgroud)
希望这有用!
Gav*_*mer 12
用于此问题的所有先前答案的方法建议更改不必要的URL,并且我认为读者应该知道替代解决方案.我使用ui-router和$ stateProvider将状态值与templateUrl相关联,templateUrl指向视图的html文件.然后,只需将$ state注入您的控制器并调用$ state.go('state-value')来更新您的视图.
angular-route和angular-ui-router之间有什么区别?
小智 5
有两种方法:
如果您使用的是ui-router $stateProvider
,请执行以下操作:
$state.go('stateName'); //remember to add $state service in the controller
Run Code Online (Sandbox Code Playgroud)
如果您使用角度路由器或$routeProvider
,请执行以下操作:
$location.path('routeName'); //similarily include $location service in your controller
Run Code Online (Sandbox Code Playgroud)