角度指令控制器范围

时间:2017-01-28 04:03:02

标签: angularjs angularjs-directive

我正在尝试开发一个拥有它自己的控制器的指令,以便它可以从API中收集所需的数据并注入任何地方。

这是我到目前为止所得到的:

public class Student
{
   public Student() {}

   public int Name{ get; set; }
   public DateTime Time{ get; set; }
   // other properties
   public string EventId {get;set;}

   public virtual Event Event {get; set; }
}

所以我喜欢的是从它自己的控制器获取数据的指令。到目前为止,我无法以这种方式工作。

有谁知道怎么做?

1 个答案:

答案 0 :(得分:1)

你可以使用这样的东西

function bookingsChart() {
    return {
        restrict: 'E',
        scope: true,
        controller: ['$scope', 'yourservice', function ($scope, yourservice) {
            $scope.data = [];   //bind this data to your view

            $scope.getServiceData = function (count) {
                //your service call
                yourservice.getServiceData(count).then(function (data) {
                    $scope.data = data;  //sets data in $scope.data variable
                });
            }
        }],
        link: function (scope, elements, attributes) {
            //if you want to pass any parameters from your UI to your service method
            scope.getServiceData(attributes.count);    //calls the getServiceData in controller defined above 
        }
     }
 }
相关问题