AngularJS:我如何切换从控制器功能的看法? [英] AngularJS : How do I switch views from a controller function?
问题描述
我想使用AngularJS的的 NG-点击的功能切换视图。我怎么会去与下面的code这样做呢?
index.html的
< DIV NG控制器=CNTRL>
< DIV NG点击=someFunction()>
点我
< DIV>
< DIV>
controller.js
函数CNTRL($范围){
$ scope.someFunction =功能(){
// code改变看法?
}
}
在以不同的视图之间切换,你可以直接改变了window.location(使用$位置的服务!)在
index.html文件
< DIV NG控制器=CNTRL>
< DIV NG点击=changeView(编辑)>
编辑
< / DIV>
< DIV NG点击=changeView('preVIEW')>
preVIEW
< / DIV>
< / DIV>
Controller.js
函数CNTRL($范围,$位置){
$ scope.changeView =功能(视图){
$ location.path(视图); //路径不能散列
}
}
和配置路由器切换到基于位置不同谐音(如这里所示<一href=\"https://github.com/angular/angular-seed/blob/master/app/app.js\">https://github.com/angular/angular-seed/blob/master/app/app.js )。这将具有历史的好处以及使用纳克 - 图。
另外,你用NG-包括不同的泛音,然后使用纳克开关,如图这里(<一href=\"https://github.com/ganarajpr/Angular-UI-Components/blob/master/index.html\">https://github.com/ganarajpr/Angular-UI-Components/blob/master/index.html )
I am trying to use the ng-click feature of AngularJS to switch views. How would I go about doing this with the code below?
index.html
<div ng-controller="Cntrl">
<div ng-click="someFunction()">
click me
<div>
<div>
controller.js
function Cntrl ($scope) {
$scope.someFunction = function(){
//code to change view?
}
}
In order to switch between different views, you could directly change the window.location (using the $location service!) in index.html file
<div ng-controller="Cntrl">
<div ng-click="changeView('edit')">
edit
</div>
<div ng-click="changeView('preview')">
preview
</div>
</div>
Controller.js
function Cntrl ($scope,$location) {
$scope.changeView = function(view){
$location.path(view); // path not hash
}
}
and configure the router to switch to different partials based on the location ( as shown here https://github.com/angular/angular-seed/blob/master/app/app.js ). This would have the benefit of history as well as using ng-view.
Alternatively, you use ng-include with different partials and then use a ng-switch as shown in here ( https://github.com/ganarajpr/Angular-UI-Components/blob/master/index.html )
这篇关于AngularJS:我如何切换从控制器功能的看法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!