繁体   English   中英

我的Submitpost功能不起作用

[英]My Submitpost function isn't working

第一次使用angularJs,我正在关注AngularJS reddit克隆教程,但我不知道哪里出了问题。 当我单击提交按钮时,没有任何反应。 https://github.com/Eibonic/AngularJS-reddit/tree/master/app

这是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());
        });
        };

});

这是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>

我看了一下您链接的github项目,有两个问题。 我通过运行项目并在Chrome中打开调试控制台找到了这两个对象。

Unknown provider: AuthProvider <- Auth <- NavCtrl在英语中,这意味着您试图将Auth提供程序(不存在)作为依赖项注入NavCtrl中。 这是在nav.js的第3行上。 您实际上尚未使用Auth服务,因此只需将其从函数声明中删除即可对其进行修复。 它看起来应该像这样:

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

一旦到达教程中创建Auth提供程序的位置,便可以在此处注入它。

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

这意味着HTML尝试加载postview.js脚本,但找不到它。 事实证明,加载脚本时,第89行的index.html中有错字。 它应该是http://localhost:9000/scripts/controllers/postview.js (在“脚本”中缺少“ t”)

进行了这两项更改后,现在看起来工作正常。

暂无
暂无

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

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