Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/471.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 指令templateUrl重定向到/_Javascript_Angularjs_Partial_Directive_Angular Ui Router - Fatal编程技术网

Javascript 指令templateUrl重定向到/

Javascript 指令templateUrl重定向到/,javascript,angularjs,partial,directive,angular-ui-router,Javascript,Angularjs,Partial,Directive,Angular Ui Router,我试图在指令中使用templateUrl加载部分,但在访问模板的URL时,我被重定向回/。因此,分部加载整个页面,而不是分部请求 add-to-cart.js .directive('addToCart', function() { return { restrict: 'E', scope: { }, replace: false, templateUrl: 'src/common/add-to-cart

我试图在指令中使用templateUrl加载部分,但在访问模板的URL时,我被重定向回/。因此,分部加载整个页面,而不是分部请求

add-to-cart.js

.directive('addToCart', function() {
    return {
        restrict: 'E',
        scope: {

        },
        replace: false,
        templateUrl: 'src/common/add-to-cart/add-to-cart.tpl.html',
        link: function(scope, element, attrs) {

        }
    };
});
app.js

$urlRouterProvider.otherwise( '/' );

为什么会发生这种情况?我如何解决它?

我要做的是使用id作为templateUrl,而不是正确的路径。我不完全确定它是否总是这样,或者是因为我使用的是grunt angular template引擎,但这一更改解决了问题:

templateUrl: 'add-to-cart/add-to-cart.tpl.html',
另一种解决方案是注入templateCache并由它加载模板,使用完全相同的id和完全相同的结果。所以总的来说,我认为它做了完全相同的事情

.directive('addToCart', ['$templateCache', function($templateCache) {
...
    templateUrl: $templateCache.get('add-to-cart/add-to-cart.tpl.html'),
...
}]);

那么,当您打开
src/common/add to cart/add to cart.tpl.html
时,它会给您提供索引文件吗?听起来您需要配置服务器以使用静态HTML资源进行响应。我开始怀疑它是否可以是grunt angular模板引擎。它会删除tpl.html文件并将其缓存在js文件中,所以考虑到缓存,我可能没有正确加载模板?