更改不会自动出现在视图上(AngularJS更新)

本文关键字:视图 AngularJS 更新 | 更新日期: 2023-09-27 18:02:32

. Net/MVC应用程序,我使用AngularJS从数据库显示记录列表。当我更新记录时,它不会在视图(html)中更新,直到我刷新页面?

视图:

<tr ng-repeat="bu in bus">
  <td>{{bu.BU_Name}}</a></td>
  <td>{{bu.BU_Info}}</a></td>
  <th><a href="javascript:void(0)" ng-click="editItem(bu)">Del</a></th>
</tr>

JS:

$scope.editItem= function (bu) {
    $http.post('/CO/DelBU', {dbunt: bu})
    .then(function (response) {
        $scope.bus = response.data;
    })
    .catch(function (e) {
        console.log("error", e);
        throw e;
    })
    .finally(function () {
        console.log("This finally block");
    });
};

MVC控制器:

[HttpPost] 
    public JsonResult DelBU(BUs dbunt)
    {
        var db = new scaleDBEntities(); 
        var burecord = db.Buses.Find(dbunt.BU_ID);
        db.Database.ExecuteSqlCommand("UPDATE dbo.BUs SET BU_Name='just a test'");
        var burecords = db.Buses.ToList();
        return Json(burecords, JsonRequestBehavior.AllowGet);
    }

所以,当我点击编辑时,除非我刷新页面,否则屏幕上的名称不会改变!我该如何解决这个问题?

谢谢

更改不会自动出现在视图上(AngularJS更新)

我认为你需要设置一个监视总线-查看这里的更多细节:

如何使用$scope。$watch和$scope。在AngularJS中$apply ?