访问在AngularJS工厂$范围是什么? [英] Accessing $scope in AngularJS factory?

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

问题描述

我是新来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;
})

我收到错误,当我分配到 $范围和使用返回的数据,反正是有,我可以返回的数据分配给 $范围

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?

推荐答案

您通常不使用 $范围里面的一个工厂,服务或供应商。通常情况下,你将返回(由 $ HTTP 返回),然后处理控制器中的承诺(在那里你确实有 $范围)。

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).success(function(response) {
        $scope.something = response.model;
    });
}

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

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