Javascript 在AngularJS工厂中访问$scope?
我刚接触AngularJS,觉得它很有趣,但我对以下情况有点不清楚Javascript 在AngularJS工厂中访问$scope?,javascript,angularjs,Javascript,Angularjs,我刚接触AngularJS,觉得它很有趣,但我对以下情况有点不清楚 app.factory('deleteFac', function($http){ var factory = {}; factory.edit = function(id){ $http.get('?controller=store&action=getDetail&id=' + id). success(function(data, status){ /**
app.factory('deleteFac', function($http){
var factory = {};
factory.edit = function(id){
$http.get('?controller=store&action=getDetail&id=' + id).
success(function(data, status){
/**
got an error on the following
when i use return data; and i get data undefined
in the controller which i get it because its doing a ajax call
you don't get data until the call first.
**/
$scope.detail = data;
})
}
return factory;
})
当我分配给
$scope
并使用返回数据时出错,我是否可以将返回数据分配给$scope
?在工厂、服务或提供商内通常不使用$scope
。通常,您会返回承诺
(由$http
返回),然后在控制器中处理该承诺(在控制器中您确实有$scope
)
控制器功能:
$scope.edit = function(id) {
deleteFac.edit(id).then(function(response) {
$scope.something = response.model;
});
}
我猜你的意思是:
app.factory('deleteFac', function($http){
var service = {};
factory.edit = function(id, success, error){
var promise = $http.get('?controller=store&action=getDetail&id=' + id);
if(success)
promise.success(success);
if(error)
promise.error(error);
};
return service;
});
然后在控制器中执行以下操作:
function MyController($scope, deleteFac){
deleteFac.edit($scope.id, function(data){
//here you have access to your scope.
});
}
以下技巧是一个非常糟糕的做法,但如果您很匆忙,可以使用它:
将
$scope
替换为:angular.element(“[ng controller=CtrlName]”)。scope()
我认为这是最干净的解决方案:
如果有问题或改进,请告诉我
(function(){
angular.controller('controllerName', controllerName);
controllerName.$inject = ['$scope', factory];
function controllerName($scope, factory){
var vm = this;
vm.data = factory.alertPopup();
}
angular.factory('factory', factory);
factory.$inject = ['externalServices'];
function factory(externalServices){
return {
returnData : returnData
}
function returnData(){
return externalServices.whatever();
}
}
})();
就我个人而言,我希望使用工厂中的作用域,因此,我将把作用域作为参数从调用factory.function()的客户端传递出去,而不是全部移出 我在尝试使用$scope.watch(…)时也遇到了同样的问题,因为我们无法直接从工厂或服务中使用$scope,但我希望以这种方式工作,因此我刚刚更新了我的函数,将scope作为参数,并让工厂的客户机发送$scope。因此,这将是我的解决方案:
var-app=angular.module(“myApp”,[]);
app.factory('MyFactory',函数($http){
变量工厂={};
//这只是我自己面临的问题。
factory.Images={};
factory.myFunction=函数(id,范围){
//这是一个如何在工厂定义中使用范围的示例
scope.details=“初始值”;
//就我而言,我在使用手表时遇到了这个问题
范围.$watch('details',函数(newValue,oldValue){
如果(旧值){
scope.log=“详细信息更新为:”+newValue;
}
});
scope.details=“我的Id是:”+Id;
};
返回工厂;
});
//控制器:工厂的客户。
app.controller(“MyController”、[“$scope”、“MyFactory”、函数($scope、MyFactory){
MyFactory.myFunction(5$scope);
}]);代码>
{{详细信息}
{{log}}
我知道这个问题很老,但这对我来说很有用
app.factory('myFactory',function(){
let toRet = {
foo: foo
}
return toRet;
function foo(){ // This function needs to use passed scope.
let $scope = toRet.$scope;
// Do stuff with $scope.
}
});
app.controller('myController',function($scope,myFactory){
myFactory.$scope = $scope;
/*
We could just pass $scope as a parameter to foo, but this is
for cases where for whatever reason, you cannot do this.
*/
myFactory.foo();
});
你没有注射$scopeUhh。。。你在说什么范围?工厂没有范围——它是一种服务。您应该让edit在AJAX调用中返回承诺,并在需要的任何具有作用域的地方调用它。您是否从控制器内部使用此工厂?如果是这样,只需将控制器的作用域作为参数传递给edit
,或接受传递数据的回调。您不能注入$scope,因为它不知道注入什么作用域。@BenjaminGruenbaum我说的是控制器中的$scope?你能给我举个例子说明如何处理这种情况吗?谢谢你的解释。@Dudi不是个好主意,请参考正确答案是的,谢谢Edwin,我的意思是,谢谢你的示例-1这是一个承诺反模式。“回复承诺不太容易出错,而且通常更可取。@BenjaminGruenbaum是否介意提供任何参考资料?当然,我以前离开过这里,但因为它非常简单,所以删除了它。这是使用承诺作为荣耀回调,更不用说冗余的if
检查了。我认为David的解决方案很有效。@EdwinDalorzo如果有人要写一本关于JS承诺的(好)书,变化是它将是约100个非常具体的人之一。我在上面列出的三个可能是该列表的前十名。Kris在JS中推广了promise,编写了令人惊叹的Q库,使这一切成为可能,Domenic在A+规范上做了大量工作,并将promise应用到ES6和DOM中,Petka编写了第一个快速promise实现,使其在NodeJS的狭小空间中真正可行。问题是这个领域在JS中发展得非常快,书籍也不会很好:)@bluebill1049这就是我在评论中的意思。在这种情况下,这是正确的答案-我会使用.done
或.then
而不是.success
,但除此之外,我认为这是好的。最好使用更新此答案,然后使用而不是success
,因为success
现在已被弃用:为什么不将$scope
作为参数传递给函数edit()
如下factory.edit=function(id,$scope){return$http.get('?controller=store&action=getDetail&id='+id);}
然后你可以在工厂函数中使用$scope
做任何你想做的事情。这看起来很脏,但可能是个小把戏:)谢谢你的回答(加上你说这是一个BADDDDD练习)这是2017。如果这个答案对你有帮助,而且你刚刚开始你的项目,你可能会考虑Vu.js。这是2019。是的……我从2017开始就使用VUE.JS。注:社区wiki在被投票时不给予声誉。
(function(){
angular.controller('controllerName', controllerName);
controllerName.$inject = ['$scope', factory];
function controllerName($scope, factory){
var vm = this;
vm.data = factory.alertPopup();
}
angular.factory('factory', factory);
factory.$inject = ['externalServices'];
function factory(externalServices){
return {
returnData : returnData
}
function returnData(){
return externalServices.whatever();
}
}
})();
app.factory('myFactory',function(){
let toRet = {
foo: foo
}
return toRet;
function foo(){ // This function needs to use passed scope.
let $scope = toRet.$scope;
// Do stuff with $scope.
}
});
app.controller('myController',function($scope,myFactory){
myFactory.$scope = $scope;
/*
We could just pass $scope as a parameter to foo, but this is
for cases where for whatever reason, you cannot do this.
*/
myFactory.foo();
});