简体   繁体   English

带有Express后端的Angular Routing templateUrl不起作用

[英]Angular Routing templateUrl with Express backend not working

I am having an issue getting a partial to display using angular routing and express. 我在使用角度路由和表达来显示局部图像时遇到问题。 I am trying to set things up so I can still use pug (formerly jade) to write shortform html. 我正在尝试进行设置,以便仍然可以使用pug(以前称为jade)编写简短的html。 As far as I can tell, everything should be working, no errors, everything is pulling correctly. 据我所知,一切应该正常工作,没有错误,一切都在正确进行。 I have most other portions of the application working (api calls, controllers, etc.) My main page is as follows: 我的应用程序的大多数其他部分(api调用,控制器等)都在工作,我的主页如下:

    doctype html
    html(ng-app='myapp')
        head
            base(href='/')
            script(src="//ajax.googleapis.com/ajax/libs/angularjs/1.5.7/angular.js")
            script(src="//ajax.googleapis.com/ajax/libs/angularjs/1.5.7/angular-route.js")

        body
             header#main

            .content(ng-view)


             script(src="/js/app.js")
             script(src="/js/controllers/adminController.js")
             script(src="/js/routes.js")

Here is my routes file 这是我的路线文件

angular.module('myapp')
.config(function ($routeProvider, $locationProvider) {
    $routeProvider
        .when('/admin', {
            templateUrl: '/templates/admin',
            controller: 'AdminController',
            caseInsensitiveMatch: true
        })
        .otherwise({
            template: '<h1>Page not found</h1>'
        })
    ;
    $locationProvider.html5Mode(true);
});

And finally, the express route: 最后,快递路线:

router.get('/templates/:name', function(req, res, next) {
     var name = 'templates/' + req.params.name;
     console.log('Express: ' + name);
     res.render(name, function(err, html){
        //this callback function can be removed. It's just for debugging.
        if(err)
            console.log("Error: " + err);
        else
            console.log("We're good!");
        res.send(html);
     });
 });

Lastly, here is the node server output: 最后,这是节点服务器的输出:

Express: index
GET /admin 304 68.962 ms - -
GET /js/app.js 304 0.994 ms - -
GET /js/controllers/adminController.js 304 0.751 ms - -
GET /js/routes.js 304 14.590 ms - -
Express: templates/admin
We're good!
GET /templates/admin 304 368.081 ms - -

As you can see, the index loads, calls the partial, and the template is getting called and rendered just as you would expect. 如您所见,索引将加载,调用部分索引,并且模板将按照您的期望进行调用和呈现。

The problem is ng-view is not getting replaced or updated. 问题是ng-view没有被替换或更新。 It all works just fine if I change the route to template instead of templateUrl, and just print out a line of text, so I know routing is working and this isn't an app configuration issue. 如果我将路由更改为模板而不是templateUrl,并且仅打印出一行文本,那么一切都很好,所以我知道路由有效,这不是应用程序配置问题。

I've been stuck on this for a few days, and without any error messages, I am completely in the the dark as to why this doesn't work. 我已经坚持了几天,并且没有任何错误消息,所以我完全不知道为什么它不起作用。

Additionally, when I check the following in my controller, I get the partial, so it is coming through properly: 另外,当我在控制器中检查以下内容时,我得到了部分内容,因此可以正常进行:

angular.module('buriedcinema.controllers')
    .controller('AdminController', ['$templateCache', function(Movies, Users, $templateCache){
    console.log($templateCache.get('templates/admin'));
}
console:
<h1> this is my partial </h1>

Fixed by adding "controllerAs:" option to my routing and referring to my controller by the object name. 通过在路由中添加“ controllerAs:”选项并通过对象名称引用控制器来解决。

.when('/admin', {
            templateUrl: 'templates/admin',
            controller: 'AdminController',
            controllerAs: 'adminCtrl',
            caseInsensitiveMatch: true
        })

The more technical explanation is that I wasn't instantiating an object with state, but trying to use my controller statically so it never updated. 更具技术性的解释是,我没有实例化具有状态的对象,而是尝试静态使用我的控制器,因此它从未更新。 Hope this can help someone else. 希望这可以帮助其他人。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM