2017-01-16 64 views
0

Im使用Browserify進行捆綁和Angularjs框架。這種情況的出現,我有具有被定義爲指令中Angularjs組件 -當需要角度分量時找不到相對路徑

var loginDirective = /*@ngInject*/ function(){ 
    return{ 
    templateUrl:'views/login/templates/loginComponent.html', 
    link:function(scope,ele,attr){ 
    } 
    } 
}; 

這工作完全當我單獨使用browserify運行的組件。現在我有一個項目「MyAPP」,我使用NPM安裝來獲取這個組件在我的項目的node_modules文件夾中。 然後我需要這個及部件,包括做DI在這樣

var angular = require('angular'); 
require ('baseComponent'); 

module.exports = angular.module('mainComponent',['baseComponent']) 

的DI工作正常的項目,但我得到一個錯誤說'views/login/templates/loginComponent.html'找不到該成分導致它開始在我的項目搜索view文件夾而不是自己的view文件夾。

如何解決此問題?

回答

0

我認爲常用的方法是將模板放入您的JavaScript代碼並手動將其加載到模板緩存中。就像這樣:

angular.module('mainComponent').run(function($templateCache) { 
    var template = '<h1> LoginComponent </h1>'; 
    $templateCache.put('loginComponent.html' , template); 
}); 

而在該指令的templateUrl應該是這樣的:

var loginDirective = /*@ngInject*/ function(){ 
    return{ 
     templateUrl: 'loginComponent.html', 
     link: function(scope,ele,attr){ 
     } 
    } 
}; 

您可以像用戶界面,引導,UI電網例如很多開源庫看到這

+0

是的,這終究是我最終做的,謝謝 –