【问题标题】:Angular ng-model on input not binding within ng-repeat输入上的 Angular ng-model 未绑定在 ng-repeat 中
【发布时间】:2015-05-01 18:40:43
【问题描述】:

我正在尝试让输入条目 {{post.title}} 在提交时发布,但只有 {{post.upvotes}} 正在发布。任何人都可以看到我在代码中看不到的内容吗?

我提交时的结果是 - 赞成票:0

当我将 {{post.title}} 更改为 {{title}} 时,它可以正常发布,但我不知道为什么它不会绑定到 ng-model="post"。

---
name: home
url: /
---
<br>
<div class="grid-container">
    <div class="grid-block">
      <div class="grid-block">
        <div id="chatBlock" class="small-12 medium-8 grid-content medium-order-1">
            <div ng-repeat="post in posts | orderBy: '-upvotes'">
              {{post.title}} - upvotes: {{post.upvotes}}
            </div>
            <br>
            <form ng-submit="addPost()">
              <input type="text" ng-model="title"></input>
              <button type="submit">Post</button>
            </form>
        </div>
        <div id="contacts" class="small-12 medium-4 grid-content medium-order-2">
            <div>{{test}}</div>
        </div>
     </div>
   </div>
</div>

app.js

(function() {
  'use strict';

  angular.module('chatter', [
    'ui.router',
    'ngAnimate',

    //foundation
    'foundation',
    'foundation.dynamicRouting',
    'foundation.dynamicRouting.animations'
  ])
    .config(config)
    .run(run)
    .controller('MainCtrl', [
      '$scope', 
      function($scope){
        $scope.test = 'Contacts';
        $scope.posts = [
        {title: 'test post', upvotes: 5}
        ];
        $scope.addPost = function(){   
          $scope.posts.push({title: $scope.title, upvotes: 0});
          $scope.title = '';
        };
}]);

  config.$inject = ['$urlRouterProvider', '$locationProvider'];

  function config($urlProvider, $locationProvider) {
    $urlProvider.otherwise('/');

    $locationProvider.html5Mode({
      enabled:false,
      requireBase: false
    });

    $locationProvider.hashPrefix('!');
  }

  function run() {
    FastClick.attach(document.body);
  }

})();

【问题讨论】:

  • $scope.posts.push({title: $scope.title, upvotes: 0}); $scope.title 是什么?
  • 在这里尝试过,似乎工作正常 - jsfiddle.net/HB7LU/13396
  • @OlegYudovich,你的小提琴确实有效,但我无法将它与我当前的代码相协调。

标签: angularjs angularjs-ng-repeat angular-ngmodel


【解决方案1】:

每当您通过 post.title 时,您都会强制绑定$scope.title,其中永远不会产生价值。我相信你应该使用post.title & post.upvotes & 在调用addPost 时你应该传递post 对象,这样在posts 数组中推送数据会更容易

        <form ng-submit="addPost(post)">
          <input type="text" ng-model="post.title"></input>
          <button type="submit">Post</button>
        </form>

控制器

    $scope.addPost = function(post){   
      $scope.posts.push({title: post.title, upvotes: post.upvotes});
      //$scope.title = '';
    };

【讨论】:

  • 这很棒!这绝对是一种更好的方法。我会赞成你的回答,但我的代表还不够高。你介意投票我的问题,以便我可以投票给像你这样的好答案吗?
  • @Todd Williams 你现在可以投票了。很高兴帮助你.. 谢谢 :)
  • 啊,其实我的声望已经够高了。
  • @ToddWilliams 是的..快乐编码..:)
  • 谢谢,这真的很有帮助。
【解决方案2】:

你可以尝试两件事:

  1. 例如,在您的控制器中初始化 $scope.title

    $scope.title = '';
    $scope.test  = 'Contacts';
    ...
    
  2. 将title作为参数传递给addPost(然后使用参数):

    <form ng-submit="addPost(title)">
      <input type="text" ng-model="title"></input>
      <button type="submit">Post</button>
    </form>
    
  3. 不要直接使用title,而是创建一个对象,例如$scope.model.title。使用对象属性比直接使用原始类型更可靠。

【讨论】:

  • 感谢您的回复。我试过了,但似乎没有用。
猜你喜欢
  • 1970-01-01
  • 2015-05-11
  • 1970-01-01
  • 1970-01-01
  • 2014-07-12
  • 1970-01-01
  • 1970-01-01
  • 2012-11-22
  • 2013-01-02
相关资源
最近更新 更多