简体   繁体   English

我的Submitpost功能不起作用

[英]My Submitpost function isn't working

First time using angularJs and I'm following an AngularJS reddit clone tutorial and I don't know where I went wrong. 第一次使用angularJs,我正在关注AngularJS reddit克隆教程,但我不知道哪里出了问题。 When I click the submit button nothing happens. 当我单击提交按钮时,没有任何反应。 https://github.com/Eibonic/AngularJS-reddit/tree/master/app https://github.com/Eibonic/AngularJS-reddit/tree/master/app

This is the nav.js controller 这是nav.js控制器

    'use strict';

app.controller('NavCtrl', function ($scope, $location, Post, Auth) 
{
$scope.post = {url: 'http://', title: ''};

$scope.submitPost = function () 
{
    Post.create($scope.post).then(function (ref) 
    {
        $scope.post = {url: 'http://', title: ''};
        $location.path('/posts/' + ref.name());
        });
        };

});

This is the nav.html view file. 这是nav.html视图文件。

   <nav class="navbar navbar-default" role="navigation">
   <!-- Brand and toggle get grouped for better mobile display -->
  <div class="navbar-header">
   <button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1">
  <span class="sr-only">Toggle navigation</span>
  <span class="icon-bar"></span>
  <span class="icon-bar"></span>
  <span class="icon-bar"></span>
   </button>
    <a class="navbar-brand" href="#">ang-news</a>
    </div>

    <!-- Collect the nav links, forms, and other content for toggling -->
    <div ng-controller="NavCtrl" class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
    <form class="navbar-form navbar-left" role="search" ng-submit="submitPost()">
  <div class="form-group">
    <input type="text" class="form-control" placeholder="Title" ng-model="post.title">
  </div>
  <div class="form-group">
  <input type="text" class="form-control" placeholder="Link"      ng-model="post.url">
  </div>
  <button type="submit" class="btn btn-default">Submit</button>
</form>
 </div><!-- /.navbar-collapse -->
</nav>

I took a look at the github project you linked, there were two issues; 我看了一下您链接的github项目,有两个问题。 I found both of them by running the project and opening the debug console in Chrome. 我通过运行项目并在Chrome中打开调试控制台找到了这两个对象。

Unknown provider: AuthProvider <- Auth <- NavCtrl In English, this means that you attempted to inject the Auth provider (which doesn't exist) as a dependency in the NavCtrl. Unknown provider: AuthProvider <- Auth <- NavCtrl在英语中,这意味着您试图将Auth提供程序(不存在)作为依赖项注入NavCtrl中。 This is on line 3 of nav.js. 这是在nav.js的第3行上。 You aren't actually using the Auth service yet, so just removing it from your function declaration fixes it. 您实际上尚未使用Auth服务,因此只需将其从函数声明中删除即可对其进行修复。 It should look like this: 它看起来应该像这样:

app.controller('NavCtrl', function ($scope, $location, Post)

Once you get to the point in the tutorial where you create the Auth provider you'll be able to inject it here. 一旦到达教程中创建Auth提供程序的位置,便可以在此处注入它。

The second error was GET http://localhost:9000/scrips/controllers/postview.js 404 第二个错误是GET http://localhost:9000/scrips/controllers/postview.js 404

This means that the HTML attempted to load the postview.js script, but couldn't find it. 这意味着HTML尝试加载postview.js脚本,但找不到它。 Turns out there was a typo in index.html on line 89 when loading the script. 事实证明,加载脚本时,第89行的index.html中有错字。 It should be http://localhost:9000/scripts/controllers/postview.js (Missing the 't' in 'scripts') 它应该是http://localhost:9000/scripts/controllers/postview.js (在“脚本”中缺少“ t”)

After making those two changes it seems to be working fine now. 进行了这两项更改后,现在看起来工作正常。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM