简体   繁体   English

AngularJS:自定义指令内的ngView

[英]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

AngularJS does not support multiple ng-view s in one app. AngularJS在一个应用程序中不支持多个ng-view If you want it - you have to use another routing engine, for example Angular UI's ui-router 如果需要-您必须使用其他路由引擎,例如Angular UI的ui-router

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组件一样对待,例如inputselect等。

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM