简体   繁体   English

ng-submit在Laravel PHP框架中不起作用

[英]ng-submit is not working in Laravel PHP framework

everyone, I have another question related to Laravel/AngularJS. 大家,我还有一个关于Laravel / AngularJS的问题。

In my mini-project, I have form which user can post question in the form, but when I click submit button, there are no requests (from inspect in Google Chrome ). 在我的小型项目中,我有一个表单,用户可以在表单中发布问题,但是当我单击“ submit按钮时,没有任何请求(来自Google Chrome inspect)。 However, I have a Log in interface, the button is working very well. 但是,我有一个Log in界面,该按钮运行良好。 I use the same logic, same html structure. 我使用相同的逻辑,相同的html结构。 And I am wondering how can I fix this problem. 我想知道如何解决此问题。

I have been debugging this for 2 hours, have been Googling for a long time. 我已经调试了2个小时,已经使用Google谷歌搜索了很长时间。 Please help me out!! 请帮帮我!!

Below are the codes. 以下是代码。

// Add question page //添加问题页面

<script type="text/ng-template" id="question.add.tpl">
    <div ng-controller="QuestionAddController" class="question-add container">
        <div class="card">
            <form name="question_add_form" ng-submit="Question.add()">
                <div class="input-group">
                    <label>Title</label>
                    <input type="text"
                           name="title"
                           ng-minlength="5"
                           ng-maxlength="255"
                           ng-model="Question.new_question.title"
                           required>
                </div>
                <div class="input-group">
                    <label>Description</label>
                    <textarea type="text"
                              name="desc"
                              ng-model="Question.new_question.desc">
                    </textarea>
                </div>
                <div class="input-group">
                    <button ng-disabled="question_add_form.$invalid"
                            class="primary"
                            type="submit">Submit</button>
                </div>
            </form>
        </div>
    </div>
</script>

// Service and Controller //服务和控制器

.service('QuestionService', [
    '$http',
    '$state',
    function ($http, $state) {
        var me = this;
        me.new_question = {};

        me.go_add_question = function () {
            console.log(1);
            $state.go('question.add');
        };

        me.add = function () {
            if (!me.new_question.title)
                return;

            $http.post('/api/question/add', me.new_question)
                .then(function (r) {
                    console.log('r', r);
                    // if (r.data.status) {
                    //     console.log(r.data);
                    //     me.new_question = {};
                    //     $state.go('home');
                    // }
                }, function (e) {
                    console.log('e', e);
                })
        }
    }
])


.controller('QuestionAddController', [
    '$scope',
    'QuestionService',
    function (QuestionService, $scope) {
        $scope.Question = QuestionService;
    }
])

in QuestionService.add() , there is no "return value" ( console.log('r', r); ) in my browser. QuestionService.add() ,我的浏览器中没有“返回值”( console.log('r', r); )。

I make sure routes and url are working fine by directly typing the address in browser. 我可以通过直接在浏览器中输入地址来确保路由和网址正常运行。 Any suggestion will be highly appreciated! 任何建议将不胜感激!

Thanks in advance!!! 提前致谢!!!

Edit: 编辑:

Besides, the button Add Question is not working, too. 此外,“ Add Question ”按钮也不起作用。 I have to use ui-sref to make it redirect to the target url, will this be the reason why the submit button is not working (like I use ui-sref not the ng-submit to redirect) 我必须使用ui-sref使其重定向到目标url,这是否就是submit按钮不起作用的原因(例如我使用ui-sref而不是ng-submit进行重定向)

// Add question //添加问题

<div class="navbar clearfix">
    <div class="container">
        <div class="fl">
            <div class="navbar-item brand">somethingHere</div>
            <form ng-submit="Question.go_add_question()" id="quick_ask" ng-controller="QuestionAddController">
                <div class="navbar-item">
                    <input ng-model="Question.new_question.title" type="text">
                </div>
                <div class="navbar-item">
                    <button ui-sref="question.add" type="submit">Add question</button> <!--ui-sref="question.add"-->
                </div>
            </form>
        </div>
        <blablabla something not important here!!!!>

Your injections in the controller is the wrong order. 您在控制器中的注射顺序错误。 Should be 应该

.controller('QuestionAddController', [
    '$scope',
    'QuestionService',
    function ($scope, QuestionService) {
        $scope.Question = QuestionService;
    }
])

EDIT: The ordering itself does not really matter, but the controller function argument ordering must match the ordering in the array. 编辑:排序本身并不重要,但是控制器函数参数的排序必须与数组中的排序匹配。 Eg 例如

Correct: 正确:

['QuestionService', 'Service2', '$scope', function(QuestionService, Service2, $scope) { ...

Incorrect: 不正确:

['QuestionService', 'Service2', '$scope', function(QuestionService, scope, Service2) { ...

The reason to define the controller with array is so that the code can be minified 用数组定义控制器的原因是可以使代码最小化

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

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