简体   繁体   English

从外部角度控制器访问变量

[英]Access an variable from outside angular controller

I'm using timetable.js together with an angular router and firebase for the backend.我正在使用timetable.js以及后端的角度路由器和 firebase。 My code looks like this:我的代码如下所示:

That is the html file that angular routes to:那是 angular 路由到的 html 文件:

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

That's the file where I handle all my functions from that router:这是我从该路由器处理所有功能的文件:

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');
     };
});

What I'm now trying to do is to run that timetable.addEvent() function outside that controller.我现在要做的是在该控制器之外运行timetable.addEvent()函数。

I hope somebody understood, what I'm trying to do and can help me.我希望有人理解我正在尝试做什么并且可以帮助我。

Thanks!谢谢!

Example of how you could use angular to do this.如何使用 angular 执行此操作的示例。 All I did was create a quick and dirty fiddle that puts your code in a directive.我所做的就是创建一个快速而肮脏的fiddle ,将您的代码放入指令中。 In the directive I added an addEvent button that for now just creates the same event each time.在指令中,我添加了一个addEvent按钮,现在每次只创建相同的事件。 You would need to update this to take in the inputs required to add an event (i'll update the fiddle later today to show you how you could do this).您需要更新它以接收添加事件所需的输入(我将在今天晚些时候更新小提琴以向您展示如何执行此操作)。

Fiddle showing all of this: http://jsfiddle.net/ncapito/kkxphvg7/小提琴显示所有这些: http : //jsfiddle.net/ncapito/kkxphvg7/

Directive Definition指令定义

  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>',

    };
  }]);

Directive Controller :指令控制器

 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');
    }

  }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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