How can I check an ng-included form's validity from the parent scope?

后端 未结 2 1839
滥情空心
滥情空心 2020-12-25 12:06

I have a subform that is shared among multiple views in my app. In one view, this subform is displayed alone with a back/continue button at the bottom that leads the user to

2条回答
  •  臣服心动
    2020-12-25 12:30

    If it's a sub-form you can just move the form-tag from the sub-form and into the main-form: updated JSFiddle

    You could also nest your forms using the ngForm-directive:

    In angular forms can be nested. This means that the outer form is valid when all of the child forms are valid as well. However browsers do not allow nesting of elements, for this reason angular provides ngForm alias which behaves identical to but allows form nesting.

    The result is a bit messy imo. I'd rather create a 'myForm'-directive with a new scope to avoid using $parent - something like:

    myApp.directive('myForm',function(){
        return{ 
            restrict:'E',
            scope:{model:'='},
            templateUrl:'/form.html',
            replace:true
        }
    });
    

    - see this JSFiddle example

提交回复
热议问题