I'm fairly new to Angular and I'm facing difficulties in communicating between parent and child controllers.
The jsfiddle is here - http://jsfiddle.net/NEuJ6/35/
What I'm doing is loading a form template based on a route with a new controller. The parent controller however has the "Save and Continue" button and I'm not able to understand on how to bind the changes in the form of the child (valid / invalid state) to the button disabled status of the parent.
In short, the problem is getting the live state of the form that lives in the child controller in the parent controller
I believe, this can be solved via a $watch, but I'm not sure how to proceed.
Controller
<div ng-controller="MainCtrl">
<ul>
<li><a href="#/new">New User</a></li>
<li><a href="#/edit">Edit USer</a></li>
<li>To continue as guest, click on submit</li>
</ul>
<ng-view></ng-view>
<button ng-click="save()">Save and continue</button>
</div>
Routes
app.config( function ( $routeProvider ) {
$routeProvider
.when('/edit', {
templateUrl: "form.html",
controller: "EditController"
})
.when('/new', {
templateUrl: "form.html",
controller: "NewController"
})
});
The second question is around loading the next step of the process. When the user clicks submit and the form is valid, in that case, I would like to change the ng-view with a new template. Is something like below recommended (in the controller)?
if (form.$valid) {
location.path('/next')
}
or should I make the continue button an a
tag and do something like
<a href="#/next" ng-click="validate()">Save and continue</a>
In both the cases, I will have a new route added.
.when('/next', {
templateUrl: "next.html",
controller: "NextController"
})