加载新页面,ng-view

时间:2014-08-15 09:31:23

标签: javascript php angularjs

我使用angularjs和php构建登录系统,并且在成功登录后,用户应该可以看到新页面。

我遇到的问题是我试图加载的视图似乎没有被加载。页面内容不会显示。

这是我的控制器:

var gameApp = angular.module("gameApp", []);

gameApp.service('link', function() {
    this.user = false;
});

gameApp.config(function($routeProvider) {
    $routeProvider

    .when('/', {
            templateUrl : 'partials/firstpage.html',
            controller  : 'firstPageCtrl'
    })

    .when('/game', {
            templateUrl : 'partials/game.html',
            controller  : 'gameCtrl'
    });

});

gameApp.controller("firstPageCtrl", function($scope,$http,link,$location) {
    $scope.doLogin = function() {
        $http.post("lib/action.php", {username: $scope.username, password: $scope.password}).success(function(data) {
            if(data) {
                link.user = data;
                console.log(link.user);
                $location.path("/game.html");
            }
        }).error(function(data) {
            console.log(data);
        });
    };
});


gameApp.controller("gameCtrl", function($scope,$http,link,$location) {
    alert("hej");
    $scope.fisk = "fisk";
    /*if(link.user) {
        $scope.message = "fisk";
        console.log(link.user);
    } else {
        $scope.message = "Ledsen fisk";
        console.log("Är inte satt");
    }*/
});

应加载的视图是game.html。

以下是我对game.html的内容:

asdasdsaaaaaaaaaaaaaaaa
<div ng-controller="gameCtrl">
    <div id="test" style="border: 1px solid #000; width: 300px; height: 300px;">
        {{message}}
        {{fisk}}
    </div>
</div>

不显示上述内容。

这是我的index.html

<!DOCTYPE html>
<html ng-app="gameApp">
<head>
<link href="css/style.css" rel="stylesheet" type="text/css">
<meta content="text/html;charset=UTF-8" http-equiv="content-type" />
</head>
<body ng-controller="firstPageCtrl">
<div id="layout">
    <div id="topcontent">
    </div>
    <div id="middlecontent">
        <div ng-view></div>
    </div>
    <div id="bottomcontent">
        {{"AngularJS"}}
    </div>
</div>
</body>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.0.8/angular.min.js"></script>
<script src="js/mastercontroller.js"></script>
</html>

1 个答案:

答案 0 :(得分:0)

尝试将$location.path("/game.html");更改为$location.path("/game");中的firstPageCtrl,使其与gameApp.config中的确切声明相符。