Angularjs Routing不工作,视图不呈现

时间:2016-03-18 02:55:28

标签: angularjs angular-routing

出于某种原因,我的angularjs路由无法正常工作。单击链接时,视图不会呈现。我在控制台中看不到任何错误。

这是我的设置

    (function(){
        var app = angular.module('myapp',[
            'ngRoute',
            //'quizcontrollers'
        ]);

        app.config(['$routeProvider',
      function($routeProvider) {
        $routeProvider.
          when('/questions', {
             controller: 'quizcontroller',
             templateUrl: 'questions.html'

            })
          // .when('/quiz/', {
          //   templateUrl: 'partials/phone-detail.html',
          //   controller: 'PhoneDetailCtrl'
          // }).
          .otherwise({
            redirectTo: '/'
             });
         }]);
  }());

quiz.js

 (function(){
      var app = angular.module("myapp",[]);
      app.controller  ("quizcontroller",function($scope,$http,$location,$window){
        $scope.message ="hello"
       })
   }())

附加quiz.php

    <html ng-app="myapp">
    <head></head>
      <body>
       <a href="#/questions">Test</a>
      <div data-ng-view=""></div>

      <script src="/path/backend/web/assets/js/angular/angular.js">     </script>
      <script src="/path/backend/web/assets/js/angular/angular-route.js">     </script>
      <script src="/path/backend/web/assets/js/angular/app.js"></script>
      <script src="/path/backend/web/assets/js/angular/app-config.js"></script>
      <script src="/teachsity/backend/web/assets/js/angular/filters/range.js"></script>
      <script src="/path/backend/web/assets/js/angular/controllers/quiz.js"></script>
     </body>
   </html>

question.html文件与app.js位于同一目录中。

2 个答案:

答案 0 :(得分:2)

你声明模块两次从控制器声明中删除。

<html ng-app="myapp">
    <head></head>

    <body>
        <script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.4.0/angular.js"></script>

        <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.4.0/angular-route.js"></script>

        <a href="#/questions">Test</a>
        <div ng-view></div>

    </body>
</html>

<script type="text/javascript">
(function()
{
        var app = angular.module('myapp',['ngRoute']);

        app.config(['$routeProvider',
        function($routeProvider) 
        {
            $routeProvider

            .when('/', 
            {
                templateUrl : 'questions.html'
            })

            .when('/questions', 
            {
                controller: 'quizcontroller',
                templateUrl: 'questions.html'
            })
            .otherwise(
            {
                redirectTo: '/'
            });
        }]);

        app.controller  ("quizcontroller",function($scope,$http,$location,$window)
        {
            $scope.message ="hello"
        })
}())
</script>

答案 1 :(得分:0)

这背后的原因是因为$routeProvider假设questions.html与您的add-quiz.php具有相同的路径。所以你必须 从

更改您的templateUrl
$routeProvider.
          when('/questions', {
             controller: 'quizcontroller',
             templateUrl: 'questions.html'

            })

$routeProvider.
              when('/questions', {
                 controller: 'quizcontroller',
                 templateUrl: '/path/backend/web/assets/js/angular/questions.html'

                })