在 AngularJS 工厂中访问 $scope?

编程入门 行业动态 更新时间:2024-10-27 02:25:05
本文介绍了在 AngularJS 工厂中访问 $scope?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时送ChatGPT账号..

我是 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屋!

更多推荐

[db:关键词]

本文发布于:2023-04-22 08:15:34,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1019737.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:工厂   AngularJS   scope

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!