我的数据绑定有效,但我的输入ng模型是't更新时更新

My data binding works but my input ng-model isn't updating when I $update

本文关键字:更新 我的 模型 有效 数据绑定 输入 ng      更新时间:2023-09-26

如果我这样做{{newStore1}}并且$scope.newStore1="猫头鹰商店";,当页面加载时,文本显示Owl Store。数据绑定很好。如果我制作一个输入框,并将其命名为newStoreName模型,那么在大多数情况下,这也是有效的。。。

{{newStore1}}
<form data-ng-submit="storeUpdate()">
        <input type="text" ng-model="newStore1">
        <input type="submit" />
</form>

新的存储名称显示为null。即使当我在文本框中键入时,{{newStore}}也会随文本更新。但当我这样做时,它会给我一个空值。

    $scope.storeUpdate = function(){

        Users.get({email:$scope.global.user.email}, function(user3) {
            user3[0].store.push($scope.newStore1); // $scope.newStore1 is working in html but is 'null' here
            user3[0].$update(function(response) {
                Users.query({}, function(users) {
                    $scope.users = users;
                    $scope.global.user = response;
                });
            });
        });   
    };

在控制器中定义:

$scope.store = {};

或者,如果你不想在控制器中声明它,那么在HTML中,你需要在高层声明模型,比如:

<div ng-model="store.name">
  {{store.name}}
  <form data-ng-submit="storeUpdate()">
          <input type="text" ng-model="store.name">
          <input type="submit" />
  </form>
<div>