【问题标题】:AngularJS : get object by id from factoryAngularJS:从工厂通过 id 获取对象
【发布时间】:2015-09-21 16:34:59
【问题描述】:

我有一个factory 从数据库中获取包含我所有客户的数组。 然后我需要按人 id 过滤这个数组,并在一个页面中只显示它的数据。

我已经有一个工作代码,但它只在controller 内,我想将它与factorydirective 一起使用,因为我不再使用ng-controller 和这个factory调用我需要显示客户数据的其他页面。

这就是我试图用我的factory 做的事情:

app.js

app.factory('mainFactory', function($http){

    var getCliente = function($scope) {
        return $http.get("scripts/php/db.php?action=get_cliente")
        .success( function(data) {
            return data;
        })
        .error(function(data) {
        });
    };

    var getDetCliente = function($scope,$routeParams) {
        getCliente();
        var mycli = data;
        var myid = $routeParams.id;
        for (var d = 0, len = mycli.length; d < len; d += 1) {
            if (mycli[d].id === myid) {
                return mycli[d];
            }
        }
        return mycli;
    };

    return {
        getCliente: getCliente,
        getDetCliente: getDetCliente
    }
});

app.directive('detClienteTable', function (mainFactory) {
    return {
        restrict: "A",
        templateUrl: "content/view/det_cliente_table.html",
        link: function($scope) {
            mainFactory.getDetCliente().then(function(mycli) {
                $scope.pagedCliente = mycli;
            })
        }
    }
});

detClient.html

<p>{{pagedCliente.name}}</p>
<p>{{pagedCliente.tel}}</p>
<p>{{pagedCliente.email}}</p>
[...more code...]

问题是,我无法在页面中显示任何数据,而且我的控制台中没有错误。

可能有什么问题?
请记住,我正在学习 AngularJS。

【问题讨论】:

  • 我做的一件事是返回 promise 本身,然后工厂的消费者定义他们自己的.success 行为。不确定这是否是“最佳实践”,但它对我有用。
  • 请注意 var mycli = data; 中的“data”未定义。

标签: angularjs promise angularjs-service angular-promise angularjs-factory


【解决方案1】:

您将getCliente 视为getDetCliente 中的同步调用。有趣的是,在您的指令中,您了解 getDetCliente 是异步的。把getCliente改成这个,在getDetCliente调用的时候把它当做异步调用:

var getCliente = function($scope) {
    return $http.get("scripts/php/db.php?action=get_cliente");
};

【讨论】:

  • 有了你的小费和其他答案,我明白我必须做什么。谢谢!
【解决方案2】:

基本上,您需要实现一个承诺链,因为查看您的代码看起来您​​将getCliente() 承诺传递给getDetCliente 方法。在这种情况下,您需要使用 .then 函数而不是使用 .success.error 这不允许您继续承诺链。在getDetCliente 函数之后,您再次需要使用.then 函数,该函数在getCliente 函数得到解决他的承诺时被调用。您的代码将使用 form it 重新格式化它并返回 mycli 结果。

代码

var getCliente = function() {
    return $http.get("scripts/php/db.php?action=get_cliente")
    .then( function(res) { //success callback
        return res.data;
    },function(err){ //error callback
       console.log(err)
    })
};

var getDetCliente = function(id) {
    return getCliente().then(function(data){
        var mycli = data;
        var myid = id;
        for (var d = 0, len = mycli.length; d < len; d += 1) {
           if (mycli[d].id === myid) {
              return mycli[d];
           }
        }
        return mycli;
    })
};

编辑

您不应将控制器 $scope 传递给将与您的指令和控制器紧密耦合的服务,并且您希望传递路由的 id 参数,然后您需要从指令服务调用中传递它

link: function($scope) {
    mainFactory.getDetCliente($routeParams.id).then(function(mycli) {
        $scope.pagedCliente = mycli;
    })
}

【讨论】:

  • 我试过了,但我得到了这样的错误:TypeError: $http.get(...).then(...).error is not a function 但是如果我在return $http.get() 函数中的return $http.get() 之后将.then 更改为.success,我只有这个错误: TypeError: Cannot read property 'id' of undefined
  • @CelsomTrindade 那是我的错..看看更新的代码..我忘记删除 .error 函数
  • 谢谢!它解决了,但我仍然无法从路由器参数中获取 id。我正在通过 $routerParam 传递 id,如下所示:/detcliente/:id 另外,我正在使用 getClient 函数在客户端主页面中显示完整表,但现在我无法再访问数据了。
  • 你应该从你的指令调用 mainFactory.getDetCliente($routeParams.id).then(function(mycli) 传递那个 id 看看我的编辑和更新的 codwe
  • 它有点工作......它正在获取 id,但运行不正常。如果我像“32”这样手动填写它,那么它可以工作,但如果我写成 32(不带引号),它就不起作用。很抱歉这些问题,但我正在学习。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-09-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多