在 AngularJS 工厂中访问 $scope? [英] Accessing $scope in AngularJS factory?

查看:28
本文介绍了在 AngularJS 工厂中访问 $scope?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 AngularJS 的新手,觉得它很有趣,但我对以下情况有点不清楚.

I am new to AngularJS and find it very interesting, but I am a bit unclear about the following situation.

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 吗?

I am getting error when I assign to $scope and use return data, is there anyway I can assign the return data to the $scope?

推荐答案

您通常不会在工厂、服务或提供者中使用 $scope.通常,您会返回 promise(由 $http 返回),然后在控制器中处理该承诺(您有 $scope).

You don't typically use $scope inside a factory, service or provider. Usually, you would return the promise (returned by $http) and then handle the promise in a controller (where you do have $scope).

factory.edit = function(id){
    return $http.get('?controller=store&action=getDetail&id=' + id);
}

控制器功能:

$scope.edit = function(id) {

    deleteFac.edit(id).then(function(response) {
        $scope.something = response.model;
    });
}

这篇关于在 AngularJS 工厂中访问 $scope?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆