[英]Accessing $scope data from view to factory in AngularJs
How can I access $scope data from view to my factory in angularjs? 如何在angularjs中从视图访问$ scope数据到我的工厂? I can access $scope.items from my controller, but when I need to use it in my factory to use the data and generate a pdf I cannot access it.
我可以从控制器访问$ scope.items,但是当我需要在工厂使用它来使用数据并生成pdf时,我将无法访问它。
angular.module('myApp', [])
.controller('myCtrl', function($scope, $http, testFactory) {
$scope.link = "http://localhost:3450/loading.html";
testFactory.all().then(
function(res){
$scope.link = res;
},
function(err){
console.log(err);
}
);
})
.factory('testFactory', function($q){
var pdfInfo = {
content: [
//data should be here...
]
};
var link = {};
function _all(){
var d = $q.defer();
pdfMake.createPdf(pdfInfo).getDataUrl(function(outputDoc){
d.resolve(outputDoc);
});
return d.promise;
}
link.all = _all;
return link;
});
I used factory when I click the generate button from my view, it will wait until the pdf is generated. 当我从视图中单击“生成”按钮时,我使用的是工厂,它将等待直到生成pdf。 Coz when I did not do it this way before, I need to click the button twice just to get the pdf generated.
因为我以前没有这样做,所以我需要单击两次按钮才能生成pdf。
I did it. 我做的。 I forgot to send the
$scope.items
to my factory. 我忘了将
$scope.items
发送到我的工厂。 So what i did is I added testFactory.all($scope.items)
in my controller instead of just plain testFactory.all()
. 所以我
testFactory.all($scope.items)
是在控制器中添加了testFactory.all($scope.items)
,而不是普通的testFactory.all()
。
Then in my factory, 然后在我的工厂
I used function _all(value)
, so I can used the values passed by the views through controller. 我使用了
function _all(value)
,因此可以使用视图通过控制器传递的值。 I am not sure if this is the proper way, but it works. 我不确定这是否正确,但是可以。 Please suggest good practice if you have.
如果有的话,请提出好的做法。
It is a bad practice to move around $scope to other services, as they may change it and effect your controller logic. 将$ scope移至其他服务是一个不好的做法,因为它们可能会更改它并影响您的控制器逻辑。 It will make a coupling between controllers to other services.
它将使控制器与其他服务耦合。 If your factory requires data from the controller, it is better to just pass those parameters to the factory's function.
如果您的工厂需要控制器提供的数据,最好将这些参数传递给工厂的功能。
EDIT: I see you managed to do that, and yes - passing $scope.items is the preferred way (and not, for example, passing $scope). 编辑:我看到你设法做到这一点,是的-传递$ scope.items是首选方式(而不是例如,传递$ scope)。
You can just pass the data to your factory as a function parameter. 您可以将数据作为功能参数传递给工厂。
angular.module('myApp', [])
.controller('myCtrl', function($scope, $http, testFactory) {
var pdfInfo = {
content: $scope.items
};
$scope.link = "http://localhost:3450/loading.html";
testFactory.all(pdfInfo).then(
function(res) {
$scope.link = res;
},
function(err) {
console.log(err);
}
);
})
.factory('testFactory', function($q) {
var link = {};
function _all(pdfInfo) {
var d = $q.defer();
pdfMake.createPdf(pdfInfo).getDataUrl(function(outputDoc) {
d.resolve(outputDoc);
});
return d.promise;
}
link.all = _all;
return link;
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.