【问题标题】:Json Data not updatingJson 数据未更新
【发布时间】:2016-07-15 11:48:11
【问题描述】:

我正在使用以下代码从 angularjs 表中删除一行:

$scope.removeRow = function (index) {
if (index === -1) {
alert("Something gone wrong");
}else{
$scope.budgetdetails.splice(index,1);
}

但是我的 json 数据和数据库没有更新。我需要做什么来更新它们中的数据?

【问题讨论】:

  • 需要使用$http 将该信息发送到服务器,服务器将更新数据库。为了做到这一点,您需要将整个对象传递给您的函数,以便您有 id 发送...然后自己索引它
  • 谢谢charlieftl。我正在读那个。有什么好的例子可以指点我吗?
  • 这会有所帮助。在你的 ajax 承诺回调中使用它stackoverflow.com/questions/15453979/…

标签: angularjs json asp.net-mvc-4


【解决方案1】:

像这样:Auto select and collect checkbox values in array

$scope.getData = function(Day) {
var index = $scope.budgetdetails.indexOf(Day);

if (index < 0) {
  $scope.budgetdetails.push(Day)
  console.log('myData', $scope.budgetdetails);
} else {
  $scope.budgetdetails.splice(index, 1)
  console.log('myData',$scope.budgetdetails);
}

}

【讨论】:

  • 嗨,普里安卡,这段代码从我的角度表中删除了行。然而,它的底线与我的代码所做的相同。我的 json 仍然没有更新,我的数据库中的行保持不变。 Mansa 建议为此使用 $http。你有什么建议吗?
【解决方案2】:

要在数据库中更新,您需要使用 $http 服务 (https://docs.angularjs.org/api/ng/service/$http ) 并且您的 Api 应该处理此代码。

 $http({
      method  : 'POST',
      url     : '//http://localhost/MyAPI/MyRoute',
      data    : $scope.budgetdetails, //forms user object
      headers : {'Content-Type': 'application/json'} 
     })
      .success(function(data) {
        if (data.errors) {
          // Showing errors.            
        } else { //success message for API
          $scope.message = data.message;
        }
      }

【讨论】:

    【解决方案3】:

    我已经想通了。我按照本教程进行操作

    Angular-js-with-ASP-NET-MVC-Insert-Update-Delete

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-12-18
      • 2011-08-31
      • 2021-06-27
      • 2021-12-13
      • 1970-01-01
      • 2020-07-08
      • 2015-02-13
      • 1970-01-01
      相关资源
      最近更新 更多