AngularJS从列表更新数据

时间:2018-07-30 15:00:24

标签: html angularjs mongodb

我遇到了一个无法解决的问题,我有一个包含名称的列表,当我选择一个名称时,它会给您提供两个首选项,供您选择是否更改首选项,然后单击“ update”按钮,它应该更新数据,但是当我回到该名称时,它会显示旧的首选项,如果刷新网页,它将显示正确的首选项。我试图用reload()方法解决此问题,但它会刷新网页,因此我必须再次选择所有内容,它的目的是:它无需刷新即可更新网页。

谢谢。

FILE.JS

angular
.module('managementController', ['routesServices'])
// Controller: User to control the management page and managing of 
user accounts
.controller('managementCtrl', function (
User,
$scope,
dateService,
Routes,
$filter,
$timeout,
$http,
$window,
$route
) {
 var app = this;
 app.reloadData = function (){
   $route.reload();
 };

app.saveDriverPref = function (username) {
  var userPrefs = [];
  if (app.currDriver === undefined) {
    alert('Please select a driver');
  } else if (app.empModel === undefined) {
    alert('Please fill out Emp Type');
  } else if (app.shiftModel === undefined) {
    alert('Please fill out Shift Pref.');
  } else {
    if (app.timeModel1 && app.timeModel2) {
      userPrefs.push({
        timePref1: app.timeModel1,
        timePref2: app.timeModel2,
        empType: app.empModel,
        shiftPref: app.shiftModel
      });
      app.currDriver.pref = userPrefs;

      if (
        app.timeModel1.value === 'N/A' &&
        app.timeModel2.value === 'N/A'
      ) {
        editDriverPrefs(app.currDriver);
        app.saveDriver = false;
      } else if (app.timeModel1.value === 'N/A') {
        alert('You must select a 1st Time Pref. to save');
      } else if (app.timeModel1 === app.timeModel2) {
        alert('1st Time Pref must be difference than 2nd Time Pref.');
      } else {
        editDriverPrefs(app.currDriver);
        app.saveDriver = false;

      };

FILE.HTML

 <div ng-controller="managementCtrl as mngmt">
  <div class="container routes">
    <div class="col-sm-6">
      <div class="form-group">
        <label>Select Route</label>
        <select class="form-control" data-ng-model="mngmt.routeSelected" data-ng-options="idx as route.type for (idx, route) in mngmt.routes track by route._id" ng-click="mngmt.popTTables()" ng-change="mngmt.updateTTables(mngmt.routeSelected)">
        </select>

    </div>
  </div>
  <div class="col-sm-6">
    <div class="form-group">
      <label>Select Driver</label>
      <select ng-disabled="mngmt.checkedAll" style="margin-top:9px;" data-ng-model="selectedDriver" data-ng-options="drivers.name for drivers in mngmt.users track by drivers._id" ng-click="mngmt.updateDriver()" ng-change="mngmt.updateDrivers(selectedDriver)" class="form-control">
      </select>
    </div>
  </div>
</div>

<div class="table-responsive">
  <table class="table">
    <thead>
      <tr id="tableRow">
        <th class="driverTab">
          <div style="text-align: center;">
            <span title="Drivers Name"><i class="fa fa-id-card fa-lg white-icon" aria-hidden="true"></i></span>
            <p class="headings">Driver Name</p>
          </div>
        </th>

    <th class="driverTab">
      <div style="text-align: center;">
        <span title="Employment Type"><i class="fa fa-briefcase fa-lg white-icon" aria-hidden="true"></i></span>
        <p class="headings">Emp. Type</p>
      </div>
    </th>
    <th class="driverTab">
      <div style="text-align: center;">
        <span title="Shift Preference"><i class="fa fa-briefcase fa-lg white-icon" aria-hidden="true"></i></span>
        <p class="headings">Shift Pref.</p>
      </div>
    </th>
    <th class="driverTab">
      <div style="text-align: center;">
        <span title="Time Preference"><i class="fa fa-clock-o fa-lg white-icon" aria-hidden="true"></i></span>
        <p class="headings">1st Time Pref.</p>
      </div>
    </th>
    <th class="driverTab">
      <div style="text-align: center;">
        <span title="Employee Type"><i class="fa fa-clock-o fa-lg white-icon" aria-hidden="true"></i></span>
        <p class="headings">2nd Time Pref.</p>
      </div>
    </th>
  </tr>
</thead>
<tbody>
  <tr>
    <td>
      <div style="text-align: center;">
        <p>{{mngmt.currDriver.name}}</p>
      </div>
    </td>

<td>
  <div class="form-group">
    <select class="form-control" ng-options="opt as opt.name for opt in mngmt.data.empType" ng-disabled="mngmt.checkedAll" ng-model="mngmt.empModel" ng-change="mngmt.selectedType(mngmt.empModel)">

    </select>
  </div>
</td>
<td>
  <div class="form-group">
    <select ng-options="opt as opt.name for opt in mngmt.data.shiftPref" ng-disabled="mngmt.checked" class="form-control" ng-model="mngmt.shiftModel" ng-change="mngmt.shiftPref(mngmt.shiftModel)">

    </select>
  </div>
</td>
<td>
  <div class="form-group">
    <select ng-disabled="mngmt.checked" class="form-control" ng-options="opt as opt.name for opt in mngmt.data.timePref" ng-model="mngmt.timeModel1" ng-change="mngmt.tm1()">
    </select>
  </div>
</td>
<td>
  <div class="form-group">
    <select ng-disabled="mngmt.checked" class="form-control" ng-options="opt as opt.name for opt in mngmt.data.timePref" ng-model="mngmt.timeModel2" ng-change="mngmt.tm1()">
    </select>
  </div>
</div>
</td>
</tr>
</tbody>
</table>
<div>
  <button ng-show="mngmt.saveDriver" class="btn btn-danger" style="float: right;"type="button" name="button" ng-click="mngmt.saveDriverPref(main.username); mngmt.reloadData()">Save Driver Preferences</button>
</div>
<div class="row show-hide-message" ng-show="mngmt.successMsg || mngmt.errorMsg">
  <div ng-class="alert">{{ mngmt.successMsg || mngmt.errorMsg }}</div>
</div>
</div>
<!-- Custom Messages -->
<div ng-controller="managementCtrl as newDriver">
  <a href=""><i ng-model="newDriver.expand" title="Add a new driver" ng-click="newDriver.addUser()" class="fa" ng-class="{'fa-plus-circle fa-4x redIcon': !newDriver.expand, 'fa-minus-circle fa-4x redIcon': newDriver.expand}"></i></a>
  <br>
  <div class="col-sm-12" >
    <form class="form-inline" name="newDriverForm" ng-show="newDriver.showTheForm" ng-submit="newDriver.newDriverUser(newDriver.newDriverData, newDriverForm.$valid)" novalidate="true">
      <div class="col-sm-2">
        <input class="form-control" type="text" name="firstName" ng-model="newDriver.newDriverData.firstName" placeholder="First Name" required="true">
      </div>
      <div class="col-sm-2">
        <input class="form-control" type="text" name="lastName" ng-model="newDriver.newDriverData.lastName" placeholder="Last Name" required="true">
      </div>
      <br><br>
      <button style="margin-left: 15px;" class="btn btn-primary mainBtn">Save New Driver</button>
    </form>
  </div>

  <div class="row show-hide-message" ng-show="newDriver.successMsg || newDriver.errorMsg">
    <div ng-class="alert">{{ newDriver.successMsg || newDriver.errorMsg }}</div>
  </div>
</div>

0 个答案:

没有答案