我正在将timetable.js以及角度路由器和Firebase用于后端。我的代码如下所示:

那是角路由到的html文件:

<div class="timetable" ng-init="initTimetable()"></div>


那是我从该路由器处理所有功能的文件:

myApp.controller('myController', function($scope) {

    $scope.initTimetable = function() {
        var timetable = new Timetable();
        timetable.setScope(8, 14);

        timetable.addLocations(['Place 1', 'Place 2', 'Place 3']);

        timetable.addEvent('Homework', 'Place 1', new Date(2016,9,10,11,45), new Date(2016,9,10,12,30));

        var renderer = new Timetable.Renderer(timetable);
        renderer.draw('.timetable');
     };
});


我现在想做的是在该控制器之外运行该timetable.addEvent()函数。

我希望有人能够理解我正在做的事情并能为我提供帮助。

谢谢!

最佳答案

如何使用angular进行此操作的示例。我所做的就是创建一个快速且肮脏的fiddle,将您的代码放入指令中。在指令中,我添加了一个addEvent按钮,该按钮现在每次仅创建相同的事件。您将需要对此进行更新以吸收添加事件所需的输入(我将在今天晚些时候更新小提琴以向您展示如何执行此操作)。

显示所有这些的小提琴:http://jsfiddle.net/ncapito/kkxphvg7/

指令定义

  angular.module('myApp').directive('timetable', [function() {
    return {
      scope: {
        locations: '='
      },
      restrict: 'E',
      replace: true,
      controller: TimetableController,
      template: '<div><div class="timetable"></div><button ng-click="addEvent()">Add Event</button></div>',

    };
  }]);


指令控制器:

 function TimetableController($scope) {
    var timetable = new Timetable();
    var renderer = new Timetable.Renderer(timetable);

    init();
    $scope.addEvent = addEvent;

    var idx = 3;

    function addEvent() {
      var newLocation = 'Place ' + ++idx;
      $scope.locations.push(newLocation);

      //add if new
      timetable.addLocations([newLocation]);
      timetable.addEvent(
        'Homework' + idx, newLocation, //need to add a ui to collect this
        new Date(2016, 9, 10, 11, 45), //need to add a ui to collect this
        new Date(2016, 9, 10, 12, 30) //need to add a ui to collect this
      );

      render();
    }

    function init() {
      timetable.setScope(8, 14);
      timetable.addLocations($scope.locations);
      timetable.addEvent('Homework', $scope.locations[0], new Date(2016, 9, 10, 11, 45), new Date(2016, 9, 10, 12, 30));

      render();
    }

    function render() {
      renderer.draw('.timetable');
    }

  }

09-20 21:46