如何在angularjs路由中动态更改templateUrl

时间:2019-05-14 01:15:22

标签: javascript php angularjs angularjs-directive angular-ui-router

我有一个简单的PHP和AngularJS页面,我试图根据我的PHP登录功能更改指定的templateUrl。例如,默认情况下,我的主页首页路由如下(在成功登录之前)

 .state('index', {
        abstract: true,
        url: "/index",
        templateUrl: "views/common/content.html",
 })

content.html文件看起来像这样:

<div id="wrapper">

<!-- Page wraper -->
<div class="gray-bg">

    <!-- Page wrapper -->
    <div ng-include="'views/common/topnavbar.html'"></div>

    <!-- Main view  -->
    <div ui-view></div>

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

</div>
<!-- End page wrapper-->

成功登录后,我想将templateUrl替换为index_content.user.html,如下所示:

<div id="wrapper">

<!-- Navigation -->
<div ng-include="'views/common/navigation.html'"></div>

<!-- Page wraper -->
<div id="page-wrapper" class="gray-bg">

    <!-- Page wrapper -->
    <div ng-include="'views/common/topnavbar.html'"></div>

    <!-- Main view  -->
    <div ui-view></div>

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

</div>
<!-- End page wrapper-->

从用户登录名(以下代码)获得有效回复后,我想替换templateUrl ...这可能吗?

    var params = {
        f: 'loginuser',
        data: $('#loginForm').serialize()
    };

    $http.post('server/functions/profile.functions.php', {f: 'login', params: params}).then(function (response) {

        if (response.data.success) {

        ** Is it possible to update the templateUrl at this point before I redirect?**

            $state.go("index.main");
        }
        else {
            $scope.login_errors = response.data.error;
        }
    });

0 个答案:

没有答案