【问题标题】:Using TemplateUrl doesn`t work使用 TemplateUrl 不起作用
【发布时间】:2016-05-30 10:51:38
【问题描述】:

您好,我的代码有问题。当我点击链接页面时不会重新加载。

这是一个 HTML 和 JS 代码:

var app=angular.module('mainApp',['ngRoute']);
app.config(function($routeProvider){
      $routeProvider
          .when('/',{
                templateUrl: 'home.html'
          })
          .when('/about',{
                templateUrl: 'about.html'
          });
		  .when('/contact',{
                templateUrl: 'contact.html'
          });
		  .otherwise({ redirectTo: '/'});
});
app.controller('mainCtrl',function($scope){
	
});
<div ng-controller="mainCtrl">
		<div>
			<nav>
				<ul>
					<li><a href="#/">Home</a></li>
					<li><a href="#/about">About us</a></li>
					<li><a href="#/contact">Contact</a></li>
				</ul>
			</nav>
		</div>
	  <br/>
		  <div ng-view >
		  
		  </div>
</div>

你知道什么是错的吗?

【问题讨论】:

  • 您正在尝试 .when 关闭分隔符 ;
  • 去掉when()后的分号
  • 如果我删除;之前。当什么都没有改变时

标签: javascript html angularjs templates routes


【解决方案1】:

试试这个

var app=angular.module('mainApp',['ngRoute']);
    app.config(function($routeProvider){
          $routeProvider
              .when('/',{
                    templateUrl: 'home.html'
              })
              .when('/about',{
                    templateUrl: 'about.html'
              })
              .when('/contact',{
                    templateUrl: 'contact.html'
              })
              .otherwise({ redirectTo: '/'});
    });
    app.controller('mainCtrl',function($scope){

    });

【讨论】:

  • 地址中的页面正在更改,但在我的 ng-view 中没有任何反应。你能帮忙吗?
【解决方案2】:

删除第二个和第三个.when 情况的分号 (;)

var app=angular.module('mainApp',['ngRoute']);
app.config(function($routeProvider){
  $routeProvider
      .when('/',{
            templateUrl: 'home.html'
      })
      .when('/about',{
            templateUrl: 'about.html'
      })
      .when('/contact',{
            templateUrl: 'contact.html'
      })
      .otherwise({ redirectTo: '/'});
});

【讨论】:

    【解决方案3】:

    打开控制台。可能您的 BASE 标签丢失了。将其添加到 HEAD 标记中。

    <base href="/" />
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-07-03
      • 2015-10-26
      • 2017-06-30
      相关资源
      最近更新 更多