[英]AngularJS: ngView inside a custom directive
I'm trying to use ng-view
inside a custom directive but it's not working and it's also not giving me any console error. 我正在尝试在自定义指令中使用
ng-view
但是它不起作用,也没有给我任何控制台错误。
This is my directive: 这是我的指令:
(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;
}
}
})();
And inside header.html
file I have a call to ng-view
just like I was calling it outside (when it was working). 在
header.html
文件中,我可以调用ng-view
,就像我在外部(工作时)调用它一样。 Is it possible to nest ngView
inside a custom directive? 是否可以在自定义指令中嵌套
ngView
?
Even if you could use it you shouldn't because is not the correct approach for Angular a directive
should be treated like a web component like input
, select
, etc. 即使您可以使用它,也不应该这样做,因为这不是Angular的正确方法,应该将
directive
当作Web组件一样对待,例如input
, select
等。
Just remember that your header.html
is just a view and can be used by pretty much anything, is just the view 请记住,您的
header.html
只是一个视图,几乎所有东西都可以使用,仅是视图
.directive('myDirective', function($timeout) {
return {
restrict: 'A',
templateUrl: 'app/shared/header/header.html',
controller: controller
});
Or (using ui-router ) 或者(使用ui-router )
$stateProvider
.state('home', {
url: '/?accountkey',
templateUrl: 'app/shared/header/header.html',
controller: controller
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.