观看父控制器形式有效性 [英] Watch form validity from parent controller

查看:120
本文介绍了观看父控制器形式有效性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有我的控制器内的形式。我希望能够在此基础上表格的有效性的设置范围变量。我已经给了形式的名称已经:

I have a form within my controller. I'd like to be able to set a scope variable based on the validity of this form. I've given the form a name already:

<div ng-controller="myCtrl">

    ...

    <form name='myForm'>
        ...
    </form>

    ...

</div>

和创建该表单的$有效属性$手表(这个位于包含这种形式的控制器):

And have created a $watch on that form's $valid property (this is located in the controller which contains this form):

$scope.$watch('myForm.$valid', function(oldVal, newVal) {
    console.log("Woo! This might be working!");
    if (newVal) {
        $scope.otherVar = true;
    } else {
        $scope.otherVar = false;
    }
}

我的日志语句只出现在页面加载,而不是形式的有效性发生变化时。 (我已经验证表单的$有效的属性时,可将发生变化:
    

{{myForm会$有效}}

My log statement ONLY appears on page load, but not when the form's validity changes. (I've verified that the form's $valid property is changing by inserting:

{{ myForm.$valid }}

我一直在四处寻找SO,从我已经看到了这个的的是工作。 (显然,什么是错的,虽然...)

I've been looking around SO and from what I've seen this should be working. (Obviously something is wrong though...)

推荐答案

我觉得你的问题是功能(OLDVAL,的newval)功能(的newval,OLDVAL)
除此之外,我无法找到你的code任何过错。
这里有一个工作plunkr例如: http://plnkr.co/edit/ur42HFcVQbpTEBkRBtdY?p= preVIEW

I think your problem is function(oldVal, newVal) should be function(newVal, oldVal). Apart from that I can't find any fault in your code. Here's a working plunkr example: http://plnkr.co/edit/ur42HFcVQbpTEBkRBtdY?p=preview

外,它可能是别的东西在你的code影响的形式,你不必是嵌套在类似的 NG-如果 NG-视图

Outside of that it's probably something else in your code affecting the form, you don't have it nested in something like an ng-if or ng-view?

这篇关于观看父控制器形式有效性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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