ng-view不显示页面,但控制器正在工作

时间:2016-01-01 04:10:48

标签: angularjs

我的代码如下:

https://plnkr.co/edit/p3lgYwMRgSg7UHNLLAP9?p=info

当我打开链接时,我无法显示main.html中的部分内容。我尝试了不同的浏览器及其相同的问题。我可以在控制台日志中看到正在调用控制器。我只是不确定为什么我的ngView无法工作。

索引文件:

    !DOCTYPE html>
<html lang="en" ng-app="Computer">

  <head>
    <meta charset="utf-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <title>Computer Solutions</title>
    <!-- Bootstrap core CSS -->
    <link href="bootstrap.min.css" rel="stylesheet" />
    <!-- Custom styles for this template -->
    <link href="style.css" rel="stylesheet" />
  </head>

  <body>
    <div class="container">
      <!-- The justified navigation menu is meant for single line per list item.
           Multiple lines will require custom code not provided by Bootstrap. -->
      <div class="masthead">
        <h3 class="text-muted">Computer Solutions</h3>
        <nav>
          <ul class="nav nav-justified">
            <li>
              <a href="#/main">Home</a>
            </li>
            <li>
              <a href="about.html">About</a>
            </li>
            <li>
              <a href="services.html">Services</a>
            </li>
            <li>
              <a href="contact.html">Contact</a>
            </li>
          </ul>
        </nav>
      </div>

      <div ng-controller="MainCtrl">
        <div ng-view></div>
      </div>

      <!-- Site footer -->
      <footer class="footer">
        <p>© 2015 Company, Inc.</p>
      </footer>
    </div>
    <!-- /container -->
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.0.0-alpha1/jquery.min.js"></script>
    <script src="https://code.angularjs.org/1.3.14/angular.js"></script>
    <script src="https://code.angularjs.org/1.3.14/angular-route.js"></script>
    <script src="script.js"></script>
  </body>
</html>

script.js文件

 var app = angular.module("Computer", ['ngRoute'])

.config(['$routeProvider', function($routeProvider){
  $routeProvider.
    when('/main',{
      templateURL: 'main.html',
      controller: 'MainCtrl'
    }).
    otherwise({redirectTo:'/main'})
}])

.controller('MainCtrl',[function(){
  console.log('This is 111the MainCtrl');
}]);

main.html中

<!-- Jumbotron -->
  <div class="jumbotron">
    <div class="row">
      <div class="col-md-4">
        <img src="http://techguystaging.com/files/computer-icon.png">
      </div>

      <div class="col-md-8">
        <h1>Computer Solutions</h1>
        <p class="lead">Get your stuff fixed here.</p>
      </div>
    </div>
  </div>

  <!-- Example row of columns -->
  <div class="row">
    <div class="col-lg-4">
      <h2>Hardware Repairs</h2>
      <p>Get your CPU fixed here along with your other tech stuff </p>
    </div>
    <div class="col-lg-4">
      <h2>Virus Removal</h2>
      <p>Destroy those trojan horses and worms</p>
   </div>
    <div class="col-lg-4">
      <h2>System Tune up</h2>
      <p>Faster better stronger.</p>
    </div>
  </div>

1 个答案:

答案 0 :(得分:0)

首先摆脱ng-controller中的index.html行,因为控制器是由路由定义实例化的(您不必显式加载它)。

插件似乎不起作用的原因是main.html的相对网址字符串。将其更改为此应该可以正常工作:

$routeProvider.
    when('/main',{
       templateUrl: './main.html',
       controller: 'MainCtrl'
    })

我在这里创建并更新了plunk:https://plnkr.co/edit/uTGiDCF18J0fvQpg1It1?p=preview