【发布时间】: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