AngularJS:我如何手动设置输入$控制器中是否有效? [英] AngularJS: How do I manually set input to $valid in controller?

查看:231
本文介绍了AngularJS:我如何手动设置输入$控制器中是否有效?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用 TokenInput 的插件,并使用AngularJS内置的表单控制器的验证。

Using the TokenInput plugin and using AngularJS built-in formController validation.

现在,我想检查字段包含文本,然后设置字段,有效的,如果它。使用该插件的问题是它会创建它自己的输入,然后一个UL +李为stlying。

Right now I'm trying to check if the field contains text, and then set field to valid if it does. The issue with using the plugin is it creates it's own input and then a ul+li for stlying.

我有机会为addItem(表格名称)和我在控制器capablities,我只是需要将其设置为$有效。

I have access to addItem (formname) and my capablities in the controller, I just need to set it to $valid.

标记。

<form class="form-horizontal add-inventory-item" name="addItem">
     <input id="capabilities" name="capabilities" token-input data-ng-model="inventoryCapabilitiesAutoComplete" data-on-add="addCapability()" data-on-delete="removeCapability()" required>
     <div class="required" data-ng-show="addItem.capabilities.$error.required" title="Please enter capability."></div>
</form>

JS。

$scope.capabilityValidation = function (capability) {
  if (capability.name !== "") {
    addItem.capabilities.$valid = true;
    addItem.capabilities.$error.required = false;
  } else {
    addItem.capabilities.$valid = false;
    addItem.capabilities.$error.required = true;
  }
};

我跑的时候TokenInput东西已经进入了capabilityValidation功能和传递的对象。

I'm running the capabilityValidation function when TokenInput has something entered and passing in the object.

编辑:

在我的输入找出了NG-模型做的东西,得到自动完成结果,这就是为什么我不能NG-有效的,因为它是基于模型的工作。

Found out ng-model on my input does stuff and gets the autocomplete results, which is why I can't get ng-valid to work since it's based on the model.

$scope.inventoryCapabilitiesAutoComplete = {
  options: {
    tokenLimit: null
  },
  source: urlHelper.getAutoComplete('capability')
};

我没有写这个自动完成实施,有另一种方式来做到这一点,我将有机会获得NG-模型ATTR和到其他地方借鉴作用?

I didn't write this autocomplete implementation, is there another way to do this where I would have access to the ng-model attr and move the model function somewhere else?

推荐答案

您不能直接更改表格的有效性。如果所有的后代的输入是有效的,该形式是有效的,如果不是,则它不是

You cannot directly change a form's validity. If all the descendant inputs are valid, the form is valid, if not, then it is not.

你应该做的是设置输入元素的有效性。像这样;

What you should do is to set the validity of the input element. Like so;

addItem.capabilities.$setValidity("youAreFat", false);

现在输入端(等的形式)是无效的。
您还可以查看哪些错误会导致失效。

Now the input (and so the form) is invalid. You can also see which error causes invalidation.

addItem.capabilities.errors.youAreFat == true;
> true

这篇关于AngularJS:我如何手动设置输入$控制器中是否有效?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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