同一页面上的角度两个应用

时间:2016-07-06 02:52:50

标签: javascript angularjs

我要做的是在一个页面上渲染两个日历,每个日历都有不同的数据集。目前第一个日历正确加载但第二个日历不会加载。它似乎甚至不会尝试加载。

我对Angular很新,所以我不确定我在做单页应用程序的概念是否允许这样做,或者我是否应该采用另一种方式。

对所有建议开放!

日历应用使用:https://github.com/mattlewis92/angular-bootstrap-calendar

  

前端(修剪下来)

<div class="col-lg-9 panel panel-default" id="Calandars">
<button class="btn dropdown" data-toggle="collapse" data-target="#userCal" data-parent="#Calandars"><i class="icon-chevron-right"></i> User Calandar </button>
<button class="btn dropdown" data-toggle="collapse" data-target="#GlobalCal" data-parent="#Calandars"><i class="icon-chevron-right"></i> Global Calandar</button>
<div class="accordion-group">
<div id="userCal" class="collapse indent">

  <!---User Calendar Configuration - Working Calendar-->
  <div ng-app="UserCal" class="textfix">
    <div ng-controller="Cal as vm">
      <h2 class="text-center">{{ vm.calendarTitle }}</h2>
      <mwl-calendar events="vm.events"
                  view="vm.calendarView"
                  view-title="vm.calendarTitle"
                  view-date="vm.viewDate"
                  on-event-click="vm.eventClicked(calendarEvent)"
                  on-event-times-changed="vm.eventTimesChanged(calendarEvent); calendarEvent.startsAt = calendarNewEventStart; calendarEvent.endsAt = calendarNewEventEnd"
                  edit-event-html="'<i class=\'glyphicon glyphicon-pencil\'></i>'"
                  delete-event-html="'<i class=\'glyphicon glyphicon-remove\'></i>'"
                  on-edit-event-click="vm.eventEdited(calendarEvent)"
                  on-delete-event-click="vm.eventDeleted(calendarEvent)"
                  cell-is-open="vm.isCellOpen"
                  day-view-start="06:00"
                  day-view-end="22:00"
                  day-view-split="30"
                  cell-modifier="vm.modifyCell(calendarCell)">
      </mwl-calendar>
    </div>
  </div>
</div>

<div id="GlobalCal" class="collapse indent">
  <!---Global Calandar Configuration -- None Working Calendar-->

    <div ng-app="UserCal" class="textfix">
      <div ng-controller="GlobalCalCon as vm">
        <h2 class="text-center">{{ vm.calendarTitle }}</h2>
        <mwl-calendar events="vm.events"
                      view="vm.calendarView"
                      view-title="vm.calendarTitle"
                      view-date="vm.viewDate"
                      on-event-click="vm.eventClicked(calendarEvent)"
                      on-event-times-changed="vm.eventTimesChanged(calendarEvent); calendarEvent.startsAt = calendarNewEventStart; calendarEvent.endsAt = calendarNewEventEnd"
                      edit-event-html="'<i class=\'glyphicon glyphicon-pencil\'></i>'"
                      delete-event-html="'<i class=\'glyphicon glyphicon-remove\'></i>'"
                      on-edit-event-click="vm.eventEdited(calendarEvent)"
                      on-delete-event-click="vm.eventDeleted(calendarEvent)"
                      cell-is-open="vm.isCellOpen"
                      day-view-start="06:00"
                      day-view-end="22:00"
                      day-view-split="30"
                      cell-modifier="vm.modifyCell(calendarCell)">
        </mwl-calendar>
      </div>
    </div>       
  </div>

  

Javascript背后

angular.module('UserCal', ['mwl.calendar', 'ui.bootstrap', 'ngAnimate'])
.controller('Cal', populateCal)
.controller('GlobalCalCon', populateGlobalCal);

function populateCal($http) {
  Do Stuff
  angular.copy(MyData, vm.events)
};

function populateGlobalCal($http) {
  Do Diffrent Stuff
  angular.copy(MyData, vm.events)
};

2 个答案:

答案 0 :(得分:0)

好吧,在这里,您使用相同的模块UserCal两次。这是您的主要SPA模块,因此它只有一次。

请将ng-app="UserCal"添加到html/body代码,然后从HTML代码中删除<div ng-app="UserCal" class="textfix">

现在两个日历都可以工作:)

喝彩!

答案 1 :(得分:0)

因为你正在使用ng-app两次,你必须在页面顶部只声明一次,或者最好的事情就是在html标签上的index.html中声明它

关于index.html:

<html ng-app="UserCal">

在当前页面:

<div ng-app="UserCal">

     <div ng-controller="Ctrl1">
            // stuff goes herr
     </div>

     <div ng-controller="Ctrl1">
            // stuff goes herr
     </div>

</div>