Javascript 在提交新文本输入时,如何通过增加数字来更新HTML中的数字?
提交新推文(文本输入)时,我需要在推文总数中添加+1: 这是我的HTML:Javascript 在提交新文本输入时,如何通过增加数字来更新HTML中的数字?,javascript,html,angularjs,input,Javascript,Html,Angularjs,Input,提交新推文(文本输入)时,我需要在推文总数中添加+1: 这是我的HTML: <!DOCTYPE html> <html lang="en" data-ng-app="Twitter"> <head> <meta charset="UTF-8"> <title>Twitter Clone</title> <link rel="stylesheet" href="style.css">
<!DOCTYPE html>
<html lang="en" data-ng-app="Twitter">
<head>
<meta charset="UTF-8">
<title>Twitter Clone</title>
<link rel="stylesheet" href="style.css">
</head>
<body>
<div id="profileArea">
<img id="profile" src="http://potdeli.webs.com/twitter.png" alt="tweet">
<p style="color:white;"><strong>Tweeto</strong></p>
<p>@TweetoTwiteech</p>
<table style="width:100%">
<tr>
<th>Tweets</th>
<th colspan="1">Following</th>
<th colspan="1">Followers</th>
</tr>
<tr>
<td>2</td>
<td>0</td>
<td>0</td>
</tr>
</table>
</div>
<div class="tweets" ng-app="" ng-controller="TweetsController">
<form method="POST" action="" ng-submit="addTweet()">
<h2>Compose new tweet</h2>
<input name="tweet" type="text" ng-model="newTweet" ng-maxlength="140" placeholder="What's happening?">
<button type="submit" value="addTweets">Tweet!</button>
</form>
<div class="tweetDisplay" ng-repeat="tweet in tweets track by $index">{{ tweet }}
</div>
</div>
</div>
<script src="//cdnjs.cloudflare.com/ajax/libs/angular.js/1.2.5/angular.min.js">
</script>
<script src="tweets.js">
</script>
</body>
</html>
我尝试了一些方法,但没有得到预期的结果,我也检查了一些类似的堆栈溢出问题,但我没有找到答案。您的表标记超出了控制器的范围。让它成为控制器作用域的一部分,然后你可以在那里打印一个计数器值,比如
$scope.tweets.length
哦,天哪,我真不敢相信我是个傻瓜!我刚才补充说:
<td>{{ tweets.length }}</td>
{{tweets.length}
而不是数字2,作为tweet的数量,并将其放在身体上:
<body ng-app="" ng-controller="TweetsController">
谢谢大家为我指明了正确的方向 您能否将您尝试过的方法之一包括进来,以便我们可以帮助您解决问题?您需要做的第一件事是将
移动到由控制器控制的HTML部分。然后您需要将2
替换为{{tweets.length}
--Edit:不要使用jQuery,应该使用angular。什么计数器值
?在哪里
?@Jorge当前的形式这是一个评论(我的一个坏拷贝),不是一个答案。(另外:这个Q是通过取Angular 101来解决的,所以它很快就会被关闭/删除)我已经更新了我的答案。克里斯:我不同意,我认为这是一个答案,尽管没有代码。第二,在发布我的答案之前,我没有阅读你的评论。我只是想在午休时间回答问题,帮助社区。
<body ng-app="" ng-controller="TweetsController">