【发布时间】:2017-03-27 08:52:01
【问题描述】:
我开始创建一个 AngularJS 表单,我的 AngularJS 应用程序的其他部分工作正常。
html 文件如下所示
<form role="form" ng-submit="submitForm()">
<div class="form-group">
<label for="fullName">Name</label>
<input type="text" id="fullName" name="fullName" class="form-control"
ng-model="employee.fullName" />
</div>
<div class="col-sm-offset-3 col-sm-9">
<input type="submit" class="btn btn-primary" value="Submit"
ng-click="submitForm()" />
</div>
</form>
控制器
var QuestionEditController = function ($location, questionService, $env) {
var employee = {};
console.log('pre employee', employee); // this works
submitForm = function () {
console.log('employee', employee); // clicking on submit button does not make it in here
};
我正在使用 ui-router,看起来像这样
.state("editquestion",
{
url: "/editquestion",
templateUrl: viewBase + "questionEdit.html",
controller: "QuestionEditController",
controllerAs: "vm"
});
};
我确实有一个简单的指令,它只是显示 html 代码模板,但我看不出这会导致什么问题,我做错了什么?
更新
好的,问题确实是 ng-click。
但是,我想知道我是否正在创建更多的“混乱”看看我在 submitForm 前面添加的其他内容 (vm.)
新 HTML
<form role="form" ng-submit="vm.submitForm()">
<div class="form-group">
<label for="fullName">Name</label>
<input type="text" id="fullName" name="fullName" class="form-control"
ng-model="vm.employee.fullName" />
</div>
<div class="col-sm-offset-3 col-sm-9">
<input type="submit" class="btn btn-primary" value="Submit"/>
</div>
</form>
控制器已更新
var QuestionEditController = function ($location, questionService, $env) {
var vm = this;
vm.employee = {};
console.log('pre employee', vm.employee);
vm.submitForm = function () {
console.log('employee', vm.employee);
};
};
我正在做的事情是需要做的,还是部分不需要?
【问题讨论】:
标签: javascript angularjs angularjs-directive angular-ui-router