php - 将条目插入数据库后如何更新模型?并刷新 View

标签 php mysql angularjs yii backend

我害羞地尝试与 Angular 和后端数据库交 friend 。

操作简单。将数据库获取到 Angular 模型。在数据库中保存条目,并删除条目。

我正在执行 DELETE 操作。当我删除从数据库加载的条目时,没关系。但是当我通过 push 方法删除新创建的行时,我得到了错误。

发生这种情况是因为在模型中缺少 id。将条目插入数据库后,我尝试从数据库中反复刷新 Angular 模型。($http.get) 但在这种情况下, View 没有刷新(仅闪烁)。我看到新条目只刷新页面F5。

帮助!

书籍

   <div ng-app="App" ng-controller="MyCtrl">


    <table class="">
        <th>
            <tr style="font-size: 20px">
                <td>ID</td>
                <td>Name</td>
                <td>Price</td>
                <td>Action</td>
            </tr>
        </th>

        <tr ng-repeat="book in books">
            <td style="width: 200px">{{book.id}}</td>
            <td style="width: 200px">{{book.name}}</td>
            <td style="width: 50px">{{book.price |currency}}</td>
            <td>
                <button ng-click="removeItem($index)">Удалить</button>
            </td>
        </tr>

        <tr>
            <td></td>
            <td><input type="text" ng-model="name"/></td>
            <td><input type="number" ng-model="price"/></td>
            <td>
                <button ng-click="addBook()">Добавить книгу</button>
            </td>
        </tr>
    </table>

</div>

<script>

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

    App.controller('MyCtrl', function ($scope, $http) {
        $http.get('http://ang:8888/index.php?r=site/api2').success(function (data) {
            $scope.books = data;
        });


        $scope.removeItem = function (index) {
            var id = $scope.books[index].id;
            $scope.books.splice(index, 1);
            $http.post('http://ang:8888/index.php?r=site/del2', {id: id});

        }




        $scope.addBook = function () {
            var newBook = ({name: $scope.name, price: $scope.price});
            $scope.books.push(newBook);

            $http.post("http://ang:8888/index.php?r=site/post2", {name: $scope.name,      price: $scope.price})
                .success(function (data, status, headers, config) {
                    console.log("inserted Successfully");
                });
        $http.get('http://ang:8888/index.php?r=site/api2').success(function (data) {
            $scope.books = data;
        });

        }
    })

最佳答案

问题是由于所有远程调用的异步性质。您按顺序调用 post 和 get 方法,而没有意识到两者本质上是同步的。所以你的帖子后面紧接着是 get。

将帖子的代码更改为

  $http.post("http://ang:8888/index.php?r=site/post2", {
          name: $scope.name,
          price: $scope.price
      }).success(function (data, status, headers, config) {
          console.log("inserted Successfully");
          $http.get('http://ang:8888/index.php?r=site/api2').success(function (data) {
              $scope.books = data;
          });
      });

在上面的代码中,您只有在发布完成后才能获取。

关于php - 将条目插入数据库后如何更新模型?并刷新 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23018253/

相关文章:

php - 自连接条件在 PHP 中失败,但在 mysql 中有效

Javascript 问题 - 在 document.getElementById 之后无法提交表单

php - PHP + MySQL +JavaScript 网络聊天

php - 我所有的 MediaWiki 页面都是空白的

javascript - 使用 ng-file-upload 和 mongo gridfs 上传文件

mysql非字母顺序?

mysql - 在 SQL 中计算 2,3 四分位数平均值

php - MySQL从一个条件下连接两个表

internet-explorer-9 - IE9 中 ng-repeat 中 AngularJS 的问题

javascript - 根据父 div 的 Canvas 数量调整 Canvas 大小