如何基于角度控制器修改HTML内容

时间:2015-07-07 18:43:42

标签: angularjs single-page-application ngroute

我正在尝试使用angularjs SPA构建登录页面和功能页面。

var flag = false;
function a() {
    var a = 0;
    for (var i = 0; i < 10000000; i++) {
        a++;
    }
    console.log("first fun finished!");
    flag = true;
};

function b() {
    var a = 0;
    for (var i = 0; i < 10000000; i++) {
        a++;
    }
    console.log("second fun finished!");
};

function c() {
    var a = 0;
    for (var i = 0; i < 10000000; i++) {
        a++;
    }
    console.log(a)
};
a();
b();
console.log("This should be good");
if (flag) { //Should wait for this value before c() is called
    c();
    console.log("third fun finished!")
}

我在index.html中有导航面板,我想根据登录修改(添加或删除)多个选项卡。我不想在每个页面中编写导航面板逻辑,因为它是可重用的。我无法找到使用额外控制器的方法,因为我使用ng-route,我想不允许我在同一页面上使用多个控制器。

这是我的index.html的html代码段:

I have following controller:
 - LoginController 
 - PredictionController

And following single page:
 - Home.html : Binded to LoginController
 - trend.html : Binded to PredictionController
 - index.html : Has no controller

以下是控制器的代码:

<body>
        <nav class="navbar navbar-inverse navbar-fixed-top">
            <div class="container-fluid">
                <div class="navbar-header">
                  <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#navbar" aria-expanded="false" aria-controls="navbar">
                    <span class="sr-only">Toggle navigation</span>
                    <span class="icon-bar"></span>
                    <span class="icon-bar"></span>
                    <span class="icon-bar"></span>
                  </button>
                  <a class="navbar-brand" href="#/">MyProject</a>

                </div>
                <div id="navbar" class="collapse navbar-collapse">
                    <ul class="nav navbar-nav">
                        <li class="active"><a href="#/">Home</a></li>
                        <li><a href="#Trend">Trend</a></li>
                    </ul>
                </div><!--/.nav-collapse -->
            </div>
        </nav>

    <div ng-app="chartApp">
        <div ng-view></div>                         
    </div>

</body>

我不确定信息是否足够,如果我遗漏了一些信息,请编辑或告诉我。

1 个答案:

答案 0 :(得分:1)

我相信您必须将ng-app指令移至正文,并创建一个NavigationController,这可能需要您的UserVerified工厂并帮助维持状态导航。

您可以明确地将ng-controller指令添加到navbar,并且您的路由器应该可以正常工作。

HTML可能看起来像这样。

<body ng-app="chartApp">

    <nav ng-controller="NavigationController" class="navbar navbar-inverse navbar-fixed-top">
        <div class="container-fluid">
            <div class="navbar-header">
                <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#navbar" aria-expanded="false" aria-controls="navbar">
                    <span class="sr-only">Toggle navigation</span>
                    <span class="icon-bar"></span>
                    <span class="icon-bar"></span>
                    <span class="icon-bar"></span>
                </button>

                <!-- Only display if $scope.loggedIn -->
                <a ng-show="loggedIn" class="navbar-brand" href="#/">MyProject</a>
            </div>

            <div id="navbar" class="collapse navbar-collapse">
                <ul class="nav navbar-nav">
                    <li class="active"><a href="#/">Home</a></li>
                    <li><a href="#Trend">Trend</a></li>
                </ul>
            </div><!--/.nav-collapse -->
        </div>
    </nav>

    <div ng-view></div>
</body>

<强> NavigationController

app.controller('NavigationController', function($scope, UserVerified) {
    $scope.$watch(function() {
        return UserVerified.bool;
    }, function(state, oldState) {
        $scope.loggedIn = state;
    });
});