【问题标题】:Load new page, ng-view加载新页面,ng-view
【发布时间】:2014-08-15 09:31:23
【问题描述】:

我正在用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>

【问题讨论】:

    标签: javascript php angularjs


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 2021-04-16
      • 2016-06-12
      • 2018-09-27
      • 2016-08-19
      • 2014-06-16
      • 1970-01-01
      • 1970-01-01
      • 2016-03-26
      • 2022-01-24
      相关资源
      最近更新 更多