如何将工厂注入控制器

时间:2018-03-16 08:26:02

标签: angularjs

我在工厂中调用函数并尝试注入控制器。但我得到一个错误说,未知的提供商。请让我知道我哪里出错了。

<!DOCTYPE html>
<style>

.selected {
  fill: red;
  stroke: brown;
}

</style>
<svg width="960" height="150"></svg>
<div>Event fired <span id="test"></span></div>
<script src="https://d3js.org/d3.v4.min.js"></script>
<script>

var fired=0;
var randomX = d3.randomUniform(0, 10),
    randomY = d3.randomNormal(0.5, 0.12),
    data = d3.range(800).map(function() { return [randomX(), randomY()]; });

var svg = d3.select("svg"),
    margin = {top: 10, right: 50, bottom: 30, left: 50},
    width = +svg.attr("width") - margin.left - margin.right,
    height = +svg.attr("height") - margin.top - margin.bottom,
    g = svg.append("g").attr("transform", "translate(" + margin.left + "," + margin.top + ")");

var x = d3.scaleLinear()
    .domain([0, 10])
    .range([0, width]);

var y = d3.scaleLinear()
    .range([height, 0]);

var brush = d3.brushX()
    .extent([[0, 0], [width, height]])
    .on("start brush", brushed)
	  .on("end", brushend);

var dot = g.append("g")
    .attr("fill-opacity", 0.2)
  .selectAll("circle")
  .data(data)
  .enter().append("circle")
    .attr("transform", function(d) { return "translate(" + x(d[0]) + "," + y(d[1]) + ")"; })
    .attr("r", 3.5);

g.append("g")
    .call(brush)
    .call(brush.move, [3, 5].map(x))
  .selectAll(".overlay")
    .each(function(d) { d.type = "selection"; }) // Treat overlay interaction as move.
    .on("mousedown touchstart", brushcentered); // Recenter before brushing.

g.append("g")
    .attr("transform", "translate(0," + height + ")")
    .call(d3.axisBottom(x));

function brushcentered() {
  var dx = x(1) - x(0), // Use a fixed width when recentering.
      cx = d3.mouse(this)[0],
      x0 = cx - dx / 2,
      x1 = cx + dx / 2;
  d3.select(this.parentNode).call(brush.move, x1 > width ? [width - dx, width] : x0 < 0 ? [0, dx] : [x0, x1]);
}

function brushed() {
  var extent = d3.event.selection.map(x.invert, x);
  dot.classed("selected", function(d) { return extent[0] <= d[0] && d[0] <= extent[1]; });
}


function brushend() {
  document.getElementById('test').innerHTML = ++fired;
//	console.log('end fired - ' + (++fired));
}
</script>

控制器:

app.factory('pdfdwn', function($scope) {
return{
download:function(){
    html2canvas(document.getElementById('export'), {
    onrendered: function (canvas) {
    var data = canvas.toDataURL();
    var docDefinition = {
      content: [{
          image: data, 
          width: 500,          
      }]
    };
      pdfMake.createPdf(docDefinition).download("Table.pdf");
    }
    });
      return download();
    }
    }
});

1 个答案:

答案 0 :(得分:2)

1.从工厂方法中删除$ scope,因为你无法注入内部。

2.从工厂中删除return download();,因为您已经返回下载。

控制器内部:

app.controller('myctrl', function($scope,pdfdwn){
  $scope.pdf = function() {
     pdfdwn.download();
   };
});