Javascript 聊天应用程序-Angular.JS的输出中出错
我正在学习MVA课程,本模块的目标是使用Angular JS构建聊天应用程序。当讲师运行代码时,输出与预期一致 预期输出: 在输入字段中输入名称和消息时,输出应显示在上述输入字段中输入的名称和消息,以及单击按钮后输入数据的时间 代码如下: HTML:Javascript 聊天应用程序-Angular.JS的输出中出错,javascript,angularjs,Javascript,Angularjs,我正在学习MVA课程,本模块的目标是使用Angular JS构建聊天应用程序。当讲师运行代码时,输出与预期一致 预期输出: 在输入字段中输入名称和消息时,输出应显示在上述输入字段中输入的名称和消息,以及单击按钮后输入数据的时间 代码如下: HTML: <html> <head> <title>Chirp</title> <script src="https://ajax.googleapis.com/ajax/libs/a
<html>
<head>
<title>Chirp</title>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.0/angular.min.js"></script>
<script src="javascripts/chirpApp.js"></script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.2/css/bootstrap.min.css">
<link rel="stylesheet" href="stylesheets/style.css">
</head>
<body ng-app="chirpApp">
<div id='main' class="container" ng-controller="mainController">
<div class="col-md-offset-2 col-md-8">
<div class="clearfix">
<form ng-Submit="post()">
<input required type="text" class="form-control" placeholder="Your name" ng-model="newPost.created_by" />
<textarea required class="form-control" maxlength="200" rows="3" placeholder="Say something" ng-model="newPost.text"></textarea>
<input class="btn submit-btn pull-right" type="submit" value="Chirp!" />
</form>
<div id="post-stream">
<h4>Chirp Feed</h4>
<div class="post" ng-repeat="post in posts | orderBy:'created_at':true" ng-class-odd="'odd'" ng-class-even="'even'">
<p>{{post.text}}</p>
<small>Posted by @{{post.created_by}}</small>
<small class="pull-right">{{post.created_at | date:"h:mma 'on' MMM d, y"}}</small>
</div>
</div>
</div>
</div>
</div>
</body>
</html>
CSS:
var app = angular.module('chirpApp', []);
app.controller('mainController', function($scope){
$scope.posts = [];
$scope.newPost = {created_by: '', text: '', created_at: ''};
$scope.post = function(){
$scope.newPost.created_at = Date.now();
$scope.posts.push($scope.newPost);
$scope.newPost = {created_by: '', text: '', created_at: ''};
};
});
body {
padding-top:70px;
width: 100%;
height: 100%;
width: 100%;
height: 100%;
font-family: "Source Sans Pro","Helvetica Neue",Helvetica,Arial,sans-serif;
}
a {
color: #00B7FF;
}
.submit-btn {
background-color: #b1dbff;
margin: 10px 0 10px 0;
}
.post {
padding: 10px;
margin-bottom: 5px;
border-radius: 5px;
}
.odd {
background-color: #f2f9ff;
}
.even {
background-color: #eceff3;
}
.form-auth {
max-width: 330px;
padding: 5px;
margin: 0 auto;
}
代码在他们的github帐户上可用,我从中复制粘贴了它,所以我认为代码没有问题
打开页面时,将显示以下内容:
{{post.text}}发布人{{post.created_}{{post.created_在|日期:“h:mma'on'MMM d,y”}
这不是期望的输出
在此方面的任何帮助都将不胜感激 页面上的输出表明
posts
数组为空。这是因为ng submit
指令中的输入错误没有导致表单提交任何数据。您应该重构HTML文件中的此行:
<form ng-Submit="post()">
为此:
<form ng-submit="post()">
这将注册
ng submit
指令并启用表单提交数据。起初,当我尝试将代码复制粘贴到自己的环境中时,它不起作用。仔细查看之后,我意识到对javascript和css文件的引用试图在不存在的文件夹中查找。由于我创建了一个项目文件夹并将所有三个文件都放在其中,因此我的参考资料如下:
Change
<script src="javascripts/chirpApp.js"></script>
<link rel="stylesheet" href="stylesheets/style.css">
To
<script src="chirpApp.js"></script>
<link rel="stylesheet" href="style.css">
更改
到
在修复了这两个之后,它工作得非常好。如果要复制代码,请确保文件名和文件夹结构相同,否则复制的引用将不起作用。您在控制台中看到任何错误吗?我刚刚用您的确切代码创建了一个Plunk,它正在按预期工作。请参见此处:
ng Submit
应为ng Submit
。中的指令属性始终使用小写markup@Yerken:当我打开页面时,会显示以下内容:{{post.text}}{{post.created}{u by}{post.created}{u在| date:h:mma'on'MMM d,y}}}当我输入数据时,页面会被刷新,输出中除了上面的textDone之外没有显示任何内容,还不走运。现在的输出是什么?页面仍然加载由{{post.created_}{{post.created_}{post.created_在|日期:'mmmd,y}上的{post.text}}{post.created_}。只是不明白为什么。您是否设置了本地web服务器?你是如何测试代码的?我已经创建了文件夹,但是现在我把所有的文件都放在一个文件夹中,效果很好。另外,模块中的.js文件中有一个输入错误。谢谢你的回复!