当angularjs使用spring时,子文件夹路由不工作。

时间:2022-03-18 11:04:35

I am using spring and angular js in my project. In spring I set the view resolver as follows,

我在我的项目中使用spring和角js。在spring中,我将视图解析器设置为如下所示,

public ViewResolver viewResolver() {
    InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();

    viewResolver.setPrefix("/WEB-INF/views/");

    return viewResolver;

}

In angularjs ,I'm trying to route views/subview/test.html. Code as follows,

在angularjs中,我尝试路由视图/子视图/test.html。代码如下,

$stateProvider

        .state("test", {url: "/test", templateUrl: "subview/test.html"});
});

Here the subview is the child folder of view folder.Since I set the view resolver to view folder only it not supports the view/subview/test.html.

这里子视图是视图文件夹的子文件夹。由于我将视图解析器设置为只查看文件夹,所以它不支持视图/子视图/test.html。

Is it possible to setPrefix to all subfolders? If not, suggest other ways to handle this problem.

是否可以对所有子文件夹设置前缀?如果没有,建议其他方法来解决这个问题。

1 个解决方案

#1


2  

no you will need to set the full path each time - the path is relative to the top level html page the app is running in - you could of course use variables to manage this e.g.

不需要你每次都设置完整的路径——这条路径相对于应用程序正在运行的顶层html页面——你当然可以使用变量来管理它。

var baseTemplatePath = "/WEB-INF/views/";

$stateProvider
    .state("test", {url: "/test", templateUrl: baseTemplatePath + "subview/test.html"});
});

you could also put the variable in a higher level service for access everywhere.

您还可以将变量放在更高级别的服务中,以便在任何地方访问。

angular.app("yourApp")
    .service("pathService", function() {
        return {
            baseTemplatePath: "/WEB-INF/views/" 
        };
    }); 

and then use (making sure you have injected the pathService):

然后使用(确保您已经注入了路径服务):

$stateProvider
    .state("test", {url: "/test", templateUrl: pathService.baseTemplatePath + "subview/test.html"});
});  

#1


2  

no you will need to set the full path each time - the path is relative to the top level html page the app is running in - you could of course use variables to manage this e.g.

不需要你每次都设置完整的路径——这条路径相对于应用程序正在运行的顶层html页面——你当然可以使用变量来管理它。

var baseTemplatePath = "/WEB-INF/views/";

$stateProvider
    .state("test", {url: "/test", templateUrl: baseTemplatePath + "subview/test.html"});
});

you could also put the variable in a higher level service for access everywhere.

您还可以将变量放在更高级别的服务中,以便在任何地方访问。

angular.app("yourApp")
    .service("pathService", function() {
        return {
            baseTemplatePath: "/WEB-INF/views/" 
        };
    }); 

and then use (making sure you have injected the pathService):

然后使用(确保您已经注入了路径服务):

$stateProvider
    .state("test", {url: "/test", templateUrl: pathService.baseTemplatePath + "subview/test.html"});
});