AngularJs不能访问窗体对象在控制器($范围) [英] AngularJs can't access form object in controller ($scope)

查看:104
本文介绍了AngularJs不能访问窗体对象在控制器($范围)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的引导,UI更具体的模态窗口。而且我有一个模态形式,我要的是实例化表单验证对象。所以基本上我这样做:

I am using bootstrap-ui more specifically modal windows. And I have a form in a modal, what I want is to instantiate form validation object. So basically I am doing this:

<form name="form">
    <div class="form-group">
        <label for="answer_rows">Answer rows:</label>
        <textarea name="answer_rows" ng-model="question.answer_rows"></textarea>
    </div>
</form>

<pre>
    {{form | json}}
</pre

我可以看到html文件的形式对象,而没有问题的,但是如果我想从控制器访问表单验证对象。这只是我的输出空对象。下面是控制器例如:

I can see form object in the html file without no problem, however if I want to access the form validation object from controller. It just outputs me empty object. Here is controller example:

.controller('EditQuestionCtrl', function ($scope, $modalInstance) {
    $scope.question = {};
    $scope.form = {};

    $scope.update = function () {
        console.log($scope.form); //empty object
        console.log($scope.question); // can see form input
    };
});

可能是什么,我不能从控制器访问 $ scope.form 的原因是什么?

推荐答案

就为了谁不使用 $范围,而此的,在他们的控制,你必须添加控制器别名preceding形式的名称。例如:

Just for those who are not using $scope, but rather this, in their controller, you'll have to add the controller alias preceding the name of the form. For example:

<div ng-controller="ClientsController as clients">
  <form name="clients.something">
  </form>
</div>

和然后在控制器上

app.controller('ClientsController', function() {
  // setting $setPristine()
  this.something.$setPristine();
};

希望它也有助于整个一套答案。

Hope it also contributes to the overall set of answers.

这篇关于AngularJs不能访问窗体对象在控制器($范围)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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