[英]ng-submit is not working in Laravel PHP framework
大家,我還有一個關於Laravel / AngularJS的問題。
在我的小型項目中,我有一個表單,用戶可以在表單中發布問題,但是當我單擊“ submit
按鈕時,沒有任何請求(來自Google Chrome
inspect)。 但是,我有一個Log in
界面,該按鈕運行良好。 我使用相同的邏輯,相同的html結構。 我想知道如何解決此問題。
我已經調試了2個小時,已經使用Google谷歌搜索了很長時間。 請幫幫我!!
以下是代碼。
//添加問題頁面
<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('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;
}
])
在QuestionService.add()
,我的瀏覽器中沒有“返回值”( console.log('r', r);
)。
我可以通過直接在瀏覽器中輸入地址來確保路由和網址正常運行。 任何建議將不勝感激!
提前致謝!!!
編輯:
此外,“ Add Question
”按鈕也不起作用。 我必須使用ui-sref
使其重定向到目標url,這是否就是submit
按鈕不起作用的原因(例如我使用ui-sref
而不是ng-submit
進行重定向)
//添加問題
<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!!!!>
您在控制器中的注射順序錯誤。 應該
.controller('QuestionAddController', [
'$scope',
'QuestionService',
function ($scope, QuestionService) {
$scope.Question = QuestionService;
}
])
編輯:排序本身並不重要,但是控制器函數參數的排序必須與數組中的排序匹配。 例如
正確:
['QuestionService', 'Service2', '$scope', function(QuestionService, Service2, $scope) { ...
不正確:
['QuestionService', 'Service2', '$scope', function(QuestionService, scope, Service2) { ...
用數組定義控制器的原因是可以使代碼最小化
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.