与变量$ HTTP范围问题 [英] $http scope issue with variable

查看:111
本文介绍了与变量$ HTTP范围问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

$http({
    url: "php/myuId.php",
    method: "POST",
    data: {
        'userId': userId,
    },
    headers: {'Content-Type': 'application/x-www-form-urlencoded'}
    }).success(function(data, status, headers, config) {

        $scope.myId= data;
//return last inserted id
    }).error(function(data, status, headers, config) {
    });
//can't use it outside?
        $scope.user.push({
        "userId":  $scope.myId,

    });

奇怪,当我用它的范围$ HTTP之外$ scope.myId是改变不了的,所以我不能把我的最后插入的ID,以前端。

strange, $scope.myId isn't change when I used it outside of scope $http, so I can't push my last inserted id to front end.

推荐答案

使用尝试。适用();在JS的变化不会被看到,还是体现在用户界面,如果你不知道。

Try using .apply(); The change in JS won't be seen, or reflected in the UI if you don't.

}).success(function(data, status, headers, config) {
    $scope.apply(function(){
        $scope.myId = data;
    });
})

这篇关于与变量$ HTTP范围问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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