角未知的提供者和无法发布/注册 [英] Angular unknown provider and Cannot POST /signup

查看:136
本文介绍了角未知的提供者和无法发布/注册的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在你说什么,我知道解决的办法可能是注入的依赖,但我检查其他开源$ C ​​$ CS,它与我相比较,它是完全一样的。虽然他们的工作,而我不工作

Before you say anything, I know the solution probably is to inject the dependency but I checked other open source codes and compare it with mine, it is exactly the same. While theirs work while mine's not working

目前的问题是,在路线'/注册

Currently the problem is at route '/signup'

我的错误:错误:unpr
未知提供商
当我检查控制台在路线'/注册以及

I got Error: error:unpr Unknown Provider when I check the console at route '/signup' and

每当我去'/注册的路线和preSS提交按钮,它应该打API和重定向到'/',但我得到的却是不能发布/注册。

Whenever I go to '/signup' route and press submit button it supposed to hit the api and redirect to '/' but what I got instead is Cannot POST /signup.

该API工作得很好,当我用的邮递员,我认为这个问题是有棱有角。

The api works just fine when I used POSTMAN, I assume the problem is with angular.

index.html的(是否脚本的顺序很重要?)

index.html ( Does the order of the script is important? )

<!DOCTYPE html> 
<html ng-app="MyApp">
<head>
    <title></title>
    <base href='/'> 
    <!-- load bootstrap from CDN and custom CSS -->
    <link rel="stylesheet" href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.2/css/bootstrap.min.css">
    <!-- <link rel="stylesheet" href="//cdnjs.cloudflare.com/ajax/libs/animate.css/3.1.1/animate.min.css"> -->
    <link rel="stylesheet" href="assets/css/bootstrap.min.css">

    <!-- JS -->
    <!-- load angular and angular-route via CDN -->
    <script src="//ajax.googleapis.com/ajax/libs/angularjs/1.3.8/angular.min.js"></script>
    <script src="//ajax.googleapis.com/ajax/libs/angularjs/1.3.8/angular-route.js"></script>

    <script src="app/app.js"></script>
    <script type="text/javascript" src="app/controllers/userController.js"></script>
    <script type="text/javascript" src="app/services/factories.js"></script>


</head>
<body>


    <div ng-view></div>

</body>
</html>

app.js

app.js

angular.module('MyApp', ['ngRoute'])

    .config(function($routeProvider, $locationProvider) {

        $routeProvider

            .when('/', {
                templateUrl: 'app/views/home.html',
                controller: 'HomeController'

            })

            .when('/signup', {
                templateUrl: 'app/views/signup.html',
                controller: 'SignUpController'
            });

        $locationProvider.html5Mode(true);  
    }) 

userController.js

userController.js

 angular.module('MyApp')

    .controller('SignUpController', function($scope, User) {


        // sign up the user
        $scope.signup = function() {

            User.create({
                name: $scope.name,
                username: $scope.username,
                password: $scope.password
            });

        }


    });

factories.js

factories.js

angular.module('MyApp')

.factory('User', function($http, $alert, $window, $location) {


    var userFactory = {};

    userFactory.create = function(userData) {
        return $http.post('/api/signup', userData)
            .then(function(data) {
                $window.localStorage.setItem('token', response.data.token);
                $location.path('/');
            });
    }


    return userFactory;

});

signup.html

signup.html

<div class="span3 well">
      <legend>New to Project626? Sign up!</legend>
    <form method="post" ng-submit="signup()">
        <input class="span3" name="name" placeholder="Full Name" type="text" ng-model="name"> 
        <input class="span3" name="username" placeholder="Username" type="text" ng-model="username">
        <input class="span3" name="password" placeholder="Password" type="password" ng-model="password"> 
        <button class="btn btn-primary" type="submit">Sign up for Project626</button>
    </form>
</div>

我喜欢写作角度的这种方式,因为它给我的方式清洁。为了您的信息,我上面提到的开源的https://github.com/sahat/tvshow-tracker/blob/master/public/controllers/signup.js

推荐答案

你几乎没有添加

<script src="angular-route.js">

订单必须是这样的:

Order must be this :

  <script src="app/app.js"></script>
  <script type="text/javascript" src="app/services/factories.js"></script>
  <script type="text/javascript" src="app/controllers/userController.js"></script>

这就是为什么它是显示未知提供商的路线。

That's why it is showing unknown provider for route.

这篇关于角未知的提供者和无法发布/注册的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆