![](/img/trans.png)
[英]Where to store remote data used by directive inside ngView? (AngularJS)
[英]AngularJS: ngView inside a custom directive
我正在嘗試在自定義指令中使用ng-view
但是它不起作用,也沒有給我任何控制台錯誤。
這是我的指令:
(function() {
'use strict';
angular
.module('app')
.directive('header', Header);
Header.$inject = ['USER_AGENT'];
function Header(USER_AGENT) {
return {
restrict: 'A',
templateUrl: 'app/shared/header/header.html',
controller: controller
}
function controller($scope) {
$scope.isMobile = USER_AGENT.isMobile;
}
}
})();
在header.html
文件中,我可以調用ng-view
,就像我在外部(工作時)調用它一樣。 是否可以在自定義指令中嵌套ngView
?
AngularJS在一個應用程序中不支持多個ng-view
。 如果需要-您必須使用其他路由引擎,例如Angular UI的ui-router
即使您可以使用它,也不應該這樣做,因為這不是Angular的正確方法,應該將directive
當作Web組件一樣對待,例如input
, select
等。
請記住,您的header.html
只是一個視圖,幾乎所有東西都可以使用,僅是視圖
.directive('myDirective', function($timeout) {
return {
restrict: 'A',
templateUrl: 'app/shared/header/header.html',
controller: controller
});
或者(使用ui-router )
$stateProvider
.state('home', {
url: '/?accountkey',
templateUrl: 'app/shared/header/header.html',
controller: controller
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.