AngularJS:如何从控制器功能切换视图?

2024-02-21

我正在尝试使用ng-clickAngularJS 的切换视图功能。我将如何使用下面的代码来做到这一点?

索引.html

 <div ng-controller="Cntrl">
        <div ng-click="someFunction()">
            click me
        <div>
    <div>

控制器.js

  function Cntrl ($scope) {
        $scope.someFunction = function(){
            //code to change view?
        }
    }

为了在不同的视图之间切换,您可以直接更改 window.location (使用 $location 服务!) 索引.html 文件

<div ng-controller="Cntrl">
        <div ng-click="changeView('edit')">
            edit
        </div>
        <div ng-click="changeView('preview')">
            preview
        </div>
</div>

控制器.js

function Cntrl ($scope,$location) {
        $scope.changeView = function(view){
            $location.path(view); // path not hash
        }
    }

并配置路由器根据位置切换到不同的部分(如下所示https://github.com/angular/angular-seed/blob/master/app/app.js 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 https://github.com/ganarajpr/Angular-UI-Components/blob/master/index.html )

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

AngularJS:如何从控制器功能切换视图? 的相关文章

随机推荐