Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 从外部角度控制器访问变量_Javascript_Angularjs_Angular Controller - Fatal编程技术网

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

Javascript 从外部角度控制器访问变量,javascript,angularjs,angular-controller,Javascript,Angularjs,Angular Controller,我正在使用一个角路由器和firebase作为后端。我的代码如下所示: 这是Angle路由到的html文件: <div class="timetable" ng-init="initTimetable()"></div> 我现在要做的是在该控制器之外运行timeline.addEvent()函数 我希望有人能理解我的意图,并能帮助我 谢谢 如何使用angular进行此操作的示例。我所做的就是创建一个快速而肮脏的小提琴,将代码放入指令中。在指令中,我添加了一个addEven

我正在使用一个角路由器和firebase作为后端。我的代码如下所示:

这是Angle路由到的html文件:

<div class="timetable" ng-init="initTimetable()"></div>
我现在要做的是在该控制器之外运行timeline.addEvent()函数

我希望有人能理解我的意图,并能帮助我


谢谢

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

小提琴展示了这一切:

指令定义

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

    };
  }]);

您是否正在从angular(服务、控制器、指令)内部校准timeline.addEvent()?我尝试的只是从另一个js文件调用timeline变量,这当然没有成功。您可以使用timeline.addEvent()来在指令中,通过从DOM传递值,您在控制器中做的太多了,请将时间表的逻辑放入服务中(以便它可以被应用程序中的其他模块和控制器使用),然后利用此控制器提供的服务。然后,控制器应仅由视图使用。如果不这样做,很可能会导致您陷入无法缩放的混乱。@ojuskulkarni也不起作用,因为我需要获取firebase的值。但是谢谢你的回答。谢谢你的回答,我明天会看的。
  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');
    }

  }