search

Home  >  Q&A  >  body text

angular.js - 关于angular路由的问题

想用angular里的routeProvider和template来做content部分的视图切换,为什么这样写并不能实现?代码如下。
PS后台非NODE

index.html

...
    <p id="content-holder">
        <p ng-view></p>
    </p>
...

route.js:


var myApp = angular.module('myApp',['ngRoute']); myApp.config(['$routeProvider',function($routeProvider){ $routeProvider .when('/',{ templateUrl:"page/home/homePage.html", controller:"contentController" }) .otherwise({ redirectTo: '/' }); }]); myApp.controller("contentController",function(){ ... });

homePage.html:

    <p id="content" data-role="content">
        ...
    </p>
高洛峰高洛峰2744 days ago565

reply all(1)I'll reply

  • 为情所困

    为情所困2017-05-15 16:54:23

    I ran it according to your code and it works. I estimate that your error may appear in the following steps:

    • That homePage.html path can you see if it is correct
    • You can check the information that appears in your console, if it is the error message below XMLHttpRequest cannot load file:///******/test/1/page/home/homePage.html. Cross origin requests are only supported for protocol schemes: http, data, chrome, chrome-extension, https, chrome-extension-resource.说明你的整个文件夹不是在一个服务器下运行,因为要加载外部的html代码,所以浏览器是禁止的,只有通过本地的一个服务器才可以运行(这个是在google chromePractice in the browser).
    • It seems to be able to run directly in the Firefox browser, and there seems to be no need to open a server.
    • Check carefully whether all the modules loaded by your code have been loaded, whether they are correct, and whether they are still thereindex.html页面中是否添加ng-app.

    That’s basically it, you can take a closer look, I hope it can help you. ^_^

    reply
    0
  • Cancelreply