简体   繁体   English

当我单击按钮时,为什么我的角度应用程序会刷新数据源中的所有行?

[英]Why does my angular app refresh all rows from the data source when I click a button?

I'm working on my first app in angularjs, and I have a problem with the app refreshing all rows from the database whenever I click on a button, even though I haven't asked it to. 我正在使用angularjs开发我的第一个应用程序,但是即使我没有要求,该应用程序也会在每次单击按钮时刷新数据库中的所有行,但存在问题。 Each row from the data source has a 'block' button. 数据源中的每一行都有一个“阻止”按钮。 If I click that button, that row in the model is marked as blocked correctly and restyled, but then all the rows are refreshed from the data source, overwriting the change on the client side. 如果单击该按钮,则模型中的该行被标记为正确阻止并重新设置了样式,但是随后所有行都从数据源刷新,从而覆盖了客户端的更改。 Why is this occurring? 为什么会这样呢?

Full code: 完整代码:

<script>

var bookingAppModule = angular.module('bookingApp',[]);

bookingAppModule.controller('bookingController', ['$scope', '$http', function ($scope, $http) {
    $scope.bookingDate = '<%= DateTime.Today.Year %>/<%= DateTime.Today.Month %>/<%= DateTime.Today.Day %>';

    $scope.getAppointments = function () {
        var response = $http.post('/api/Appointments', "'" + $scope.bookingDate + "'" );
        response.success(function (data, status, headers, config) {
            $scope.appointments = data;
        });
        response.error(function (data, status, headers, config) {
            alert('Something went wrong...');
        });
    };

    $scope.appointments = $scope.getAppointments();

    $scope.blockSlot = function (slot) {
        slot.blocked = true;
    };

    $scope.bookSlot = function (slot) {
        alert("booking slot " + slotId);
    }
}]);


</script>
<div id ="booking-app" ng-app="bookingApp">
<div id="service-select">
<label for='<%= ddlService.ClientID %>'>Select service:</label>
<asp:DropDownList ID="ddlService" runat="server" DataTextField="Description" DataValueField="Id" OnSelectedIndexChanged ="ddlService_SelectedIndexChanged" AutoPostBack="false" >
</asp:DropDownList>
</div>
<div ng-controller="bookingController">
    <input type="date" ng-model="bookingDate" ng-change="getAppointments()" />
    <div class ="appt-day">
    <div ng-repeat="shift in appointments">
    <div class="appt-counsellor">
        <h2>{{shift.counsellor}}</h2>
    </div>
    <div ng-repeat ="slot in shift.slots" class="appt-row clearfix" ng-class="{'blocked' : slot.blocked}" >
        <div class="appt-time">{{slot.time}}</div>
        <div class="appt-desc">{{slot.clientName}}</div>
        <button class="appt-btn mla-button" ng-class="{'show' : !slot.blocked}" ng-click="bookSlot(slot)">Book</button>
        <button class="appt-btn mla-button" ng-class="{'show' : !slot.blocked}" ng-click ="blockSlot(slot)">Block</button>
    </div>
    </div>
</div>
</div>

This happens because on button click you rewrite your $scope.appointments, and 'blocked' property does not save. 发生这种情况是因为在单击按钮时您重写了$ scope.appointments,并且'blocked'属性没有保存。 For solve this, you can store 'blocked' property locally in other array and on calling $scope.getAppointments write this property to every 'appointments' object. 为了解决这个问题,您可以在其他数组中本地存储“ blocked”属性,并在调用$ scope.getAppointments时将此属性写入每个“ appointments”对象。 Another way, refresh only appoitments, that has change on server - and your 'blocked' property will saved. 另一种方法是,仅刷新服务器上已更改的appoappments-并且将保存“ blocked”属性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么我使用 Datatables 的 Rails 应用程序在我使用后退按钮时无法正确绘制表格,但在我刷新页面时却如此? - Why does my rails app that uses Datatables not draw tables correctly when I use the back button, but does when I refresh the page? 为什么单击按钮时按钮的样式会发生变化? - Why does my button's style change when I click on it? 为什么来自我的 api 的数据只显示一次,当我刷新页面时它会出错 - Why does data from my api only display once and when I refresh the page it gives an error 为什么单击按钮后我的数据文件无法更改? - Why my data file cannot change when I click on the button? 当我单击关闭表单的按钮时,为什么我的 HTML 网页会刷新 - Why would my HTML webpage refresh when i click a button that closes a form 单击按钮时需要刷新角度应用程序 - Need to refresh angular app on button click 当我单击一个按钮时,页面不会在 React Javascript 中刷新 - When I click a button the page does not refresh in React Javascript 当我点击按钮时,它什么也没做 - My button does nothing when i click on it 为什么单击搜索时页面会刷新? - Why does page refresh when i click search? 为什么我的 if 语句在我手动更改选中状态时有效,但在单击按钮时无效 - Why does my if statement work if I manually change the checked state, but not when I click the button
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM