繁体   English   中英

单击按钮即可更改视图

[英]Changing view upon button click

我试图在单击按钮时显示不同的视图。 这是我目前拥有的东西,但是当我单击按钮时,什么都没有改变,我留在主页上。 我正在尝试使用$ location.path('/ path');

app.js

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

    app.config(function ($routeProvider) {
        $routeProvider
            .when("/", { templateUrl: "views/news.html", controller: "mainCtrl"})
            .when("/hello", { templateUrl: "views/hello.html", controller: "helloCtrl"})

    });

app.controller("mainCtrl", function ($scope) {
    $scope.setNews = () => { 
        $scope.showHello = false;
        $scope.newsactive = "butactive"
        $scope.helloactive = ""
    };
});

    app.controller("helloCtrl", function ($scope) {
        $scope.setHello = () => { 
            $location.path('/hello');
            $scope.showHello = false;
            $scope.helloactive = "butactive"
            $scope.newsactive = ""
        };
    });

的index.html

<body ng-app="app" ng-controller="mainCtrl">
    <div>
        <div ng-include="'views/title.html'"></div>
        <div ng-include="'views/navbar.html'"></div>
    </div>

        <div ng-view></div>

    <div ng-include="'views/footer.html'"></div>

</body>

navbar.html

<div class="navbar">
    <button id="newsbutton" ng-click="setNews()" ng-class="newsactive">News</button>    
    <button id="aboutbutton" ng-click="setHello()" ng-class="helloactive">Hello</button>    
</div>

hello.html的

<div class="content">
    <div ng-hide="showHello" id="hello">
        <h1>Hello</h1>
    </div>
</div>

移动setHello你的内功能mainCtrl

目前,您试图访问setHello里面定义的函数helloCtrl从具有的范围HTML mainCtrl 更正范围将起作用。

希望这可以帮助 :)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM