AngularJS:我如何切换从控制器功能的看法? [英] AngularJS : How do I switch views from a controller function?

查看:152
本文介绍了AngularJS:我如何切换从控制器功能的看法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用AngularJS的的 NG-点击的功能切换视图。我怎么会去与下面的code这样做呢?

index.html的

 < D​​IV NG控制器=CNTRL>
        < D​​IV NG点击=someFunction()>
            点我
        < D​​IV>
    < D​​IV>

controller.js

 函数CNTRL($范围){
        $ scope.someFunction =功能(){
            // code改变看法?
        }
    }


解决方案

在以不同的视图之间切换,你可以直接改变了window.location(使用$位置的服务!)在
index.html文件

 < D​​IV NG控制器=CNTRL>
        < D​​IV NG点击=changeView(编辑)>
            编辑
        < / DIV>
        < D​​IV 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆