forms - AngularJS:刷新/重新加载数据并更新 DOM 并使表单保持原始状态

标签 forms angularjs

我撞到了另一堵墙!已经在这几个小时了。

我有一个从 JSON 文件中提取的产品列表,用于购物车系统。

当客户将产品添加到购物车时,我想刷新 JSON 提要,因为我已将其设置为根据客户在购物车中拥有的内容来调整库存水平,这样他们就不能添加太多商店没有的产品有。

“添加到购物车”运行良好。但我不知道如何“推送”更新的提要来更新 DOM 元素。

这是我到目前为止所拥有的:

$scope.reloadData = function(detailSection) {
    // Find the URL title of the current click product
    var prod_detail_url = $scope.products[detailSection].url_title;
    var prod_category_id = $scope.products[detailSection].discount_category;
    var prod_sku = $scope.products[detailSection].title;

    // Set the defaults of the info to load in
    var detailurl = '/order/series_detail/' + prod_detail_url + '/' + prod_category_id + '/' + prod_sku;
    $scope.productDetail = [];

    // Process the details of the product
    $scope.reloadedDetails = function (data, status) {
        $scope.productDetail = data;
        $scope.productDetail.push(data);
        $('.detailloader').hide();
    }

    // Fetch the Details of the product
    $scope.reloadDetail = function () {
        $('.detailloader').show();
        $http.get(detailurl).success($scope.reloadedDetails);
    }

    // Instantiate the process
    $scope.reloadDetail();
};

$scope.relatedDetail 正在触发,我的 .detailloader 正在显示。据我从检查员看到的,它正在获取 URL。 $scope.reloadedDetails,但它不会推送新数据并隐藏 .detailloader。

我“相信”我还需要在提交后再次使表格“原始”?我还没有正确地研究这个。当前的测试允许我将一件东西添加到购物车,但如果我再去添加尝试添加另一个,表单不会提交。

对此有何建议?我可以在新数据加载后告诉 Angular 再次使表单保持原始状态吗?我不确定我这里有没有右端。

非常感谢您抽出时间提供帮助。

最佳答案

试试,$scope.$apply()在您的 reloadedDetails 内功能。

您正在通过 reloadedDetails作为成功函数,所以这将在另一个范围内。

关于forms - AngularJS:刷新/重新加载数据并更新 DOM 并使表单保持原始状态,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19860964/

相关文章:

javascript - 将现有单选按钮更改为 <a href>

php - 使用 Symfony2 3.0+ 将 EntityManager 注入(inject)嵌入式 FormTypes

javascript - 如何在 AngularJS 中自动加载依赖项?

javascript - HTML 下拉元素点击后消失

javascript - 将对象数组的特定值转换为字符串

javascript - 如何从异步 API 返回 AngularJS promise

javascript - 使用 javascript 停止表单提交

jQuery .css 不适用于 chrome 但适用于 FF、Safari、IE9

javascript - 提示数字或字符仅在文本框中输入值?在 AngularJS 中如何?

javascript - 无法使用 jquery 检索文本框的值