通过ui-view加载时jquery布局无法正常工作

时间:2015-04-23 19:05:57

标签: angularjs uiview

我使用JQuery布局创建了一个布局。如果我在普通索引文件中使用它,它工作正常。但是当我尝试通过ui-view目录加载时,它没有加载。请帮忙。

我的index.html`

<html ng-app="sample">

<head>
<title>sample</title>
<link type="text/css" rel="stylesheet" href=""/>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.8/angular.min.js"></script>
<script src="jquery.js"></script>
<script src="jquery.ui.all.js"></script>
<script src="jquery.layout.js"></script>
<script src="angular-animate.js"></script>
<script src="angular-ui-router.min.js"></script>

<script type="text/javascript">
var mylayout;
$(document).ready(function(){
myLayout = $('#container').layout({west_size:400,
west_minSize:100
});
})
</script>
</head>
<body>
<div ui-view></div>
</body>

<script type="text/javascript" src="app.js">
<script type="text/javascript" src="home.js">
</html>

`

my app.js as bellow;
var SP ={};
SP.CONTROLLERS = angular.module('spControllers',[]);
sp.Dependencies =['spControllers',ui.router,'ngAnimate'
        ];

spModule = angular.module('spModule',SP.dependencies)
.config(['$stateProvider','$urlRouterProvider','$httpProvider',
function($stateProvider,$urlRouterProvider,$httpProvider){

$stateProider.
    state('home',{
        url:'',
        templateurl:'home.html'
    })
}]);

我的home.html如下;

<div id="container" ng-controller="smpCTRL">
<div class="pane ui-layout-west">WEST</div>
<div class="pane ui-layout-center">CENTER</div>
</div>

我的home.js如下;

   SP>CONTROLLERS.controller("smpCTRL", ['$scope',function($scope){
console.log("sucess");
}]);

如下css;

#container {
        background: #999;
        height:     100%;
        margin:     0 auto;
        width:      100%;
        max-width:  900px;
        min-width:  700px;
        _width:     700px; /* min-width for IE6 */
    }
    .pane {
        display:    none; /* will appear when layout inits */
    }


/*
 * Default Layout Theme
 *
 * Created for jquery.layout 
 *
 * Copyright (c) 2010 
 *   Fabrizio Balliano (http://www.fabrizioballiano.net)
 *   Kevin Dalman (http://allpro.net)
 *
 * Dual licensed under the GPL (http://www.gnu.org/licenses/gpl.html)
 * and MIT (http://www.opensource.org/licenses/mit-license.php) licenses.
 *
 * Last Updated: 2010-02-10
 * NOTE: For best code readability, view this with a fixed-space font and tabs equal to 4-chars
 */

/*
 *  DEFAULT FONT
 *  Just to make demo-pages look better - not actually relevant to Layout!
 */
body {
    font-family: Geneva, Arial, Helvetica, sans-serif;
    font-size:   100%;
    *font-size:  80%;
}

/*
 *  PANES & CONTENT-DIVs
 */
.ui-layout-pane { /* all 'panes' */
    background: #FFF; 
    border:     1px solid #BBB;
    padding:    10px; 
    overflow:   auto;
    /* DO NOT add scrolling (or padding) to 'panes' that have a content-div,
       otherwise you may get double-scrollbars - on the pane AND on the content-div
       - use ui-layout-wrapper class if pane has a content-div
       - use ui-layout-container if pane has an inner-layout
    */
    }
    /* (scrolling) content-div inside pane allows for fixed header(s) and/or footer(s) */
    .ui-layout-content {
        padding:    10px;
        position:   relative; /* contain floated or positioned elements */
        overflow:   auto; /* add scrolling to content-div */
    }

/*
 *  UTILITY CLASSES
 *  Must come AFTER pane-class above so will override
 *  These classes are NOT auto-generated and are NOT used by Layout
 */
.layout-child-container,
.layout-content-container {
    padding:    0;
    overflow:   hidden;
}
.layout-child-container {
    border:     0; /* remove border because inner-layout-panes probably have borders */
}
.layout-scroll {
    overflow:   auto;
}
.layout-hide {
    display:    none;
}

/*
 *  RESIZER-BARS
 */
.ui-layout-resizer  { /* all 'resizer-bars' */
    background:     #DDD;
    border:         1px solid #BBB;
    border-width:   0;
    }
    .ui-layout-resizer-drag {       /* REAL resizer while resize in progress */
    }
    .ui-layout-resizer-hover    {   /* affects both open and closed states */
    }
    /* NOTE: It looks best when 'hover' and 'dragging' are set to the same color,
        otherwise color shifts while dragging when bar can't keep up with mouse */
    .ui-layout-resizer-open-hover , /* hover-color to 'resize' */
    .ui-layout-resizer-dragging {   /* resizer beging 'dragging' */
        background: #C4E1A4;
    }
    .ui-layout-resizer-dragging {   /* CLONED resizer being dragged */
        border:      1px solid #BBB;
    }
    .ui-layout-resizer-north-dragging,
    .ui-layout-resizer-south-dragging {
        border-width:   1px 0;
    }
    .ui-layout-resizer-west-dragging,
    .ui-layout-resizer-east-dragging {
        border-width:   0 1px;
    }
    /* NOTE: Add a 'dragging-limit' color to provide visual feedback when resizer hits min/max size limits */
    .ui-layout-resizer-dragging-limit { /* CLONED resizer at min or max size-limit */
        background: #E1A4A4; /* red */
    }

    .ui-layout-resizer-closed-hover { /* hover-color to 'slide open' */
        background: #EBD5AA;
    }
    .ui-layout-resizer-sliding {    /* resizer when pane is 'slid open' */
        opacity: .10; /* show only a slight shadow */
        filter:  alpha(opacity=10);
        }
        .ui-layout-resizer-sliding-hover {  /* sliding resizer - hover */
            opacity: 1.00; /* on-hover, show the resizer-bar normally */
            filter:  alpha(opacity=100);
        }
        /* sliding resizer - add 'outside-border' to resizer on-hover 
         * this sample illustrates how to target specific panes and states */
        .ui-layout-resizer-north-sliding-hover  { border-bottom-width:  1px; }
        .ui-layout-resizer-south-sliding-hover  { border-top-width:     1px; }
        .ui-layout-resizer-west-sliding-hover   { border-right-width:   1px; }
        .ui-layout-resizer-east-sliding-hover   { border-left-width:    1px; }

/*
 *  TOGGLER-BUTTONS
 */
.ui-layout-toggler {
    border: 1px solid #BBB; /* match pane-border */
    background-color: #BBB;
    }
    .ui-layout-resizer-hover .ui-layout-toggler {
        opacity: .60;
        filter:  alpha(opacity=60);
    }
    .ui-layout-toggler-hover , /* need when NOT resizable */
    .ui-layout-resizer-hover .ui-layout-toggler-hover { /* need specificity when IS resizable */
        background-color: #FC6;
        opacity: 1.00;
        filter:  alpha(opacity=100);
    }
    .ui-layout-toggler-north ,
    .ui-layout-toggler-south {
        border-width: 0 1px; /* left/right borders */
    }
    .ui-layout-toggler-west ,
    .ui-layout-toggler-east {
        border-width: 1px 0; /* top/bottom borders */
    }
    /* hide the toggler-button when the pane is 'slid open' */
    .ui-layout-resizer-sliding  .ui-layout-toggler {
        display: none;
    }
    /*
     *  style the text we put INSIDE the togglers
     */
    .ui-layout-toggler .content {
        color:          #666;
        font-size:      12px;
        font-weight:    bold;
        width:          100%;
        padding-bottom: 0.35ex; /* to 'vertically center' text inside text-span */
    }

/*
 *  PANE-MASKS
 *  these styles are hard-coded on mask elems, but are also 
 *  included here as !important to ensure will overrides any generic styles
 */
.ui-layout-mask {
    border:     none !important;
    padding:    0 !important;
    margin:     0 !important;
    overflow:   hidden !important;
    position:   absolute !important;
    opacity:    0 !important;
    filter:     Alpha(Opacity="0") !important;
}
.ui-layout-mask-inside-pane { /* masks always inside pane EXCEPT when pane is an iframe */
    top:        0 !important;
    left:       0 !important;
    width:      100% !important;
    height:     100% !important;
}
div.ui-layout-mask {}       /* standard mask for iframes */
iframe.ui-layout-mask {}    /* extra mask for objects/applets */

/*
 *  Default printing styles
 */
@media print {
    /*
     *  Unless you want to print the layout as it appears onscreen,
     *  these html/body styles are needed to allow the content to 'flow'
     */
    html {
        height:     auto !important;
        overflow:   visible !important;
    }
    body.ui-layout-container {
        position:   static !important;
        top:        auto !important;
        bottom:     auto !important;
        left:       auto !important;
        right:      auto !important;
        /* only IE6 has container width & height set by Layout */
        _width:     auto !important;
        _height:    auto !important;
    }
    .ui-layout-resizer, .ui-layout-toggler {
        display:    none !important;
    }
    /*
     *  Default pane print styles disables positioning, borders and backgrounds.
     *  You can modify these styles however it suit your needs.
     */
    .ui-layout-pane {
        border:     none !important;
        background:  transparent !important;
        position:   relative !important;
        top:        auto !important;
        bottom:     auto !important;
        left:       auto !important;
        right:      auto !important;
        width:      auto !important;
        height:     auto !important;
        overflow:   visible !important;
    }
}

4 个答案:

答案 0 :(得分:1)

这就是我解决问题的方法:

  1. 在你的app.js中,创建在控制器上,
  2. 将控制器名称添加到索引主体。
  3. 将模板js转换为方法。
  4. 然后在app.js控制器中创建一个范围方法。
  5. 在范围方法中,调用模板js方法。
  6. 在ui-view html模板或仪表板内。使用ng-init使用<div ng-init='$parent.appTemplate()'></div>调用app.js控制器的范围方法。
  7. 以下是工作示例(index.html):

    <!DOCTYPE html>
    <!--[if lt IE 7]>      <html lang="en" ng-app="zeusWeb" class="no-js lt-ie9 lt-ie8 lt-ie7"> <![endif]-->
    <!--[if IE 7]>         <html lang="en" ng-app="zeusWeb" class="no-js lt-ie9 lt-ie8"> <![endif]-->
    <!--[if IE 8]>         <html lang="en" ng-app="zeusWeb" class="no-js lt-ie9"> <![endif]-->
    <!--[if gt IE 8]><!--> <html lang="en" ng-app="zeusWeb" class="no-js"> <!--<![endif]-->
    <head>
      <meta charset="utf-8">
      <meta http-equiv="X-UA-Compatible" content="IE=edge">
      <title>Screening Manager</title>
      <meta name="keywords" content="education, institution, management, portal,screening,application">
      <meta name="description" content="education, institution, management, portal,screening,application">
      <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=0">
      <meta name="author" content="K-Devs System Solutions">
      <meta name="owner" content="Kazeem Olanipekun">
      <meta name="verified by" content="K-Devs System Solutions">
      <meta name="googlebot" content="noodp">
      <meta name="google" content="translate">
      <meta name="revisit-after" content="1 month">
      <!-- build:css css/main.css-->
      <!-- bower:css -->
      <link rel="stylesheet" href="bower_components/angular-loading-bar/build/loading-bar.css" />
      <link rel="stylesheet" href="bower_components/font-awesome/css/font-awesome.css" />
      <link rel="stylesheet" href="bower_components/bootstrap/dist/css/bootstrap.css" />
      <link rel="stylesheet" href="bower_components/bootstrap-daterangepicker/daterangepicker.css" />
      <link rel="stylesheet" href="bower_components/malihu-custom-scrollbar-plugin/jquery.mCustomScrollbar.css" />
      <link rel="stylesheet" href="bower_components/animate.css/animate.css" />
      <link rel="stylesheet" href="bower_components/iCheck/skins/flat/blue.css" />
      <link rel="stylesheet" href="bower_components/nprogress/nprogress.css" />
      <link rel="stylesheet" href="bower_components/bootstrap-progressbar/css/bootstrap-progressbar-3.3.4.css" />
      <link rel="stylesheet" href="bower_components/switchery/dist/switchery.css" />
      <!--endbower-->
      <!--custom:css-->
      <link href="template.css" rel="stylesheet">
      <link rel="stylesheet" href="app.css">
      <!-- endcustom  css-->
      <!-- endbuild -->
      <link rel="shortcut icon" href="images/screening.ico" type='image/x-icon'/>
      <link rel="icon" href="images/screening.ico" type="image/x-icon"/>
        <!-- HTML5 shim and Respond.js for IE8 support of HTML5 elements and media queries -->
        <!--[if lt IE 9]>
        <script src="https://oss.maxcdn.com/html5shiv/3.7.2/html5shiv.min.js"></script>
        <script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
        <![endif]-->
    </head>
    <body data-ng-controller="zeusWebCtrl" class="nav-md footer_fixed">
      <!--[if lt IE 7]>
          <p class="browsehappy">You are using an <strong>outdated</strong> browser. Please <a href="http://browsehappy.com/">upgrade your browser</a> to improve your experience.</p>
      <![endif]-->
    
      <data ui-view ng-cloak></data>
    
      <!-- build:js js/vendors.js-->
      <!-- bower:js -->
      <script src="bower_components/jquery/dist/jquery.js"></script>
      <script src="bower_components/angular/angular.js"></script>
      <script src="bower_components/angular-mocks/angular-mocks.js"></script>
      <script src="bower_components/angular-bootstrap/ui-bootstrap-tpls.js"></script>
      <script src="bower_components/angular-ui-router/release/angular-ui-router.js"></script>
      <script src="bower_components/angular-loading-bar/build/loading-bar.js"></script>
      <script src="bower_components/bootstrap/dist/js/bootstrap.js"></script>
      <script src="bower_components/moment/moment.js"></script>
      <script src="bower_components/bootstrap-daterangepicker/daterangepicker.js"></script>
      <script src="bower_components/jquery-mousewheel/jquery.mousewheel.js"></script>
      <script src="bower_components/malihu-custom-scrollbar-plugin/jquery.mCustomScrollbar.js"></script>
      <script src="bower_components/iCheck/icheck.js"></script>
      <script src="bower_components/nprogress/nprogress.js"></script>
      <script src="bower_components/bootstrap-progressbar/bootstrap-progressbar.js"></script>
      <script src="bower_components/transitionize/dist/transitionize.js"></script>
      <script src="bower_components/fastclick/lib/fastclick.js"></script>
      <script src="bower_components/switchery/dist/switchery.js"></script>
      <!-- endbower -->
      <!-- endbuild -->
    
    
      <!-- build:js js/main.js-->
      <!-- code inclusion-->
      <script src="template.js"></script>
      <script src="scripts/index.js"></script>
      <script src="scripts/dashboard/dashboard.js"></script>
      <script src="app.js"></script>
      <!--end inclusion -->
      <!-- endbuild -->
    
    </body>
    </html>
    

    这是app.js:

        'use strict';
        // Declare app level module which depends on views, and components
        var zeusWeb=angular.module('zeusWeb', ['ui.router', 'angular-loading-bar','dashboard']);
        zeusWeb.config(['$stateProvider', '$urlRouterProvider','cfpLoadingBarProvider',function ($stateProvider, $urlRouterProvider,cfpLoadingBarProvider) {
    
          $urlRouterProvider.otherwise("/");
          /**
           *   State for the very first page of the app. This is the home page .
           */
          $stateProvider.state('home', {
            url: "/",
            templateUrl: 'views/dashboard/dashboard.html',
            controller: 'dashboardCtrl'
          });
        /*
          $stateProvider.state('dashboard', {
                url:'/dashboard',
                templateUrl: 'views/dashboard/dashboard.html',
                controller: 'dashboardCtrl',
                controllerAs:'vm'
          });*/
    
        /*
          $stateProvider.state('dashboard', {
            views:{
              'body':{
                url:'/embed',
                templateUrl: 'view1/embed.html',
                controller: 'embed',
                controllerAs:'vm'
              }
            }
          });*/
    
        }]);
        zeusWeb.controller('zeusWebCtrl',['$scope',function ($scope) {
          $scope.test = "Testing";
          $scope.appTemplate=function () {
          template();
          };
    
        }]);
    

    以下是embed ui-view dashboard.html:

        <div  class="container body">
            <div class="main_container">
                <!--sidebar-->
                <section  data-ng-include="'views/dashboard/sidebar-nav.html'" data-ng-controller="sideBarCtrl as vm"></section>
                <!--sidebar-->
    
                <!-- top navigation -->
                <section  data-ng-include="'views/dashboard/header.html'"></section>
                <!-- /top navigation -->
    
                <!-- page content -->
                <div class="right_col" role="main" ui-view="body">
                    hey
                  <!--  <script>template();</script>-->
                </div>
                <!-- /page content -->
                <!--footer-->
                <section  data-ng-include="'views/dashboard/footer.html'"></section>
                <!--footer-->
            </div>
        </div>
        <div data-ng-init="$parent.appTemplate()"></div>
    

    请注意,如果您正在使用基于角色的异步注入sidebars html。确保你有一个默认的侧边栏html,它会放置这个<div data-ng-init="$parent.appTemplate()"></div>而不是仪表板,并确保在所有其他侧边栏成功加载后最后加载默认侧边栏。

    在我的例子中,这是一个侧边栏示例(学校管理员html侧边栏):

    <div  class="menu_section">
        <h3>Live On</h3>
        <ul class="nav side-menu">
            <li><a><i class="fa fa-bug"></i> Additional Pages <span class="fa fa-chevron-down"></span></a>
                <ul class="nav child_menu">
                    <li><a href="e_commerce.html">E-commerce</a></li>
                    <li><a href="projects.html">Projects</a></li>
                    <li><a href="project_detail.html">Project Detail</a></li>
                    <li><a href="contacts.html">Contacts</a></li>
                    <li><a href="profile.html">Profile</a></li>
                </ul>
            </li>
            <li><a><i class="fa fa-windows"></i> Extras <span class="fa fa-chevron-down"></span></a>
                <ul class="nav child_menu">
                    <li><a href="page_403.html">403 Error</a></li>
                    <li><a href="page_404.html">404 Error</a></li>
                    <li><a href="page_500.html">500 Error</a></li>
                    <li><a href="plain_page.html">Plain Page</a></li>
                    <li><a href="login.html">Login Page</a></li>
                    <li><a href="pricing_tables.html">Pricing Tables</a></li>
                </ul>
            </li>
            <li><a><i class="fa fa-sitemap"></i> Multilevel Menu <span class="fa fa-chevron-down"></span></a>
                <ul class="nav child_menu">
                    <li><a href="#level1_1">Level One</a>
                    <li><a>Level One<span class="fa fa-chevron-down"></span></a>
                        <ul class="nav child_menu">
                            <li class="sub_menu"><a href="level2.html">Level Two</a>
                            </li>
                            <li><a href="#level2_1">Level Two</a>
                            </li>
                            <li><a href="#level2_2">Level Two</a>
                            </li>
                        </ul>
                    </li>
                    <li><a href="#level1_2">Level One</a>
                    </li>
                </ul>
            </li>
            <li><a href="javascript:void(0)"><i class="fa fa-laptop"></i> Landing Page <span class="label label-success pull-right">Coming Soon</span></a></li>
        </ul>
    </div>
    

    然后,假设这是用户的最后一个侧边栏,然后您调用默认侧边栏来调用该函数(默认侧边栏html):

        <div data-ng-init="$parent.appTemplate()"></div>
    

答案 1 :(得分:0)

你应该在角度DOM准备好之后尝试你的代码

angular.element(document).ready(function () {
    myLayout = $('#container').layout({west_size:400,
      west_minSize:100
    });
});

答案 2 :(得分:0)

问题是在元素#container存在之前调用.layout

正确的解决方案是为'layout'函数创建一个指令,并将该指令应用于要设置布局的元素。

答案 3 :(得分:0)

将您的脚本包含在home.html部分模板中,该模板应该有效,遇到类似的问题,我的猜测是,当您的函数最初执行时,该特定的dom元素不存在。

<强> home.html的

<script type="text/javascript">
var mylayout;
$(document).ready(function(){
myLayout = $('#container').layout({west_size:400,
west_minSize:100
});
})
</script>

<div id="container" ng-controller="smpCTRL">
<div class="pane ui-layout-west">WEST</div>
<div class="pane ui-layout-center">CENTER</div>
</div>