Looks like I’ve got it working now: I had to make the <form> element the first child in my <view> element. Must be that the <content> directive is messing with the scope?
Here is what I did in case anyone else has this issue:
@DiscGolfer17 & @max Actually, I have just discovered this same problem. Previously, I was using forms inside directives. Now that I have a form that is on a route, I can’t access the form Controller in $scope.
As mentioned, if the form is the first element of the route, it is accessible from the controller. Otherwise, it does not exist.
Open the console and switch between pages. If you open up the logged scopes, you’ll see the Home Page scope has no property ‘userForm’. the About page scope does have ‘userForm’.
I’m also noticing some scope variables that shouldn’t be on the Home Page scope, like leftButtons, navTitle, rightButtons, and scrollView. Made an issue here: https://github.com/driftyco/ionic/issues/483
I think I’m seeing a different but related problem. After upgrading to 0.9.21 from 0.9.19 my scope variables when accessed from a controller are always undefined.
Type a phone number into the phone number text box. Notice that the two way binding works since whatever you enter is repeated below the button. However, when you hit the “login” button, it prints out $scope.phoneNumber and it appears to be undefined. Not sure what’s wrong because this used to work correctly when I was using 0.9.19. Any help would be appreciated! Thanks!
I think the problem has to do with how Angular creates a new scope for directives that request an isolated scope. I was able to get this to work if I initialize my scope variable as an object:
.controller('testController', function($scope) {
// This will not be updated
$scope.testVar = '';
// This will be updated
$scope.otherTestVar = { value: '' };
});
Because <content> is a directive with an isolated scope, it will lose the reference to $scope.testVar but not $scope.otherTestVar because it is an object.
@DiscGolfer17 and @max thanks for the help! I’ve updated the test case so that it’s properly working. Maybe it’ll help someone else who has the same issue!
One thing I had to do that was unexpected was to initialize my models in the controller like so:
$scope.userInput = {};
Before I didn’t have to do this. Makes sense why this is required after reading the article @DiscGolfer17 linked to.
Maybe I’m missing something, but I think this conversation departed from the original point. The original point was that the FORM controller is not accessible from the scope in the controller.
Yes, $scope inheritance is a common cause of problems with user variables. And yes, initializing as Objects helps solve this frequently. But doing this still doesn’t allow the controller to access the form.
From the HomeController, even after initializing the “member” object, the “userForm” is still not visible. Is this simply a matter of inheritance as well? Is the only solution is to have controllers tied to content beyond the route controller or to put all logic in directive controllers?
@Calendee Yeah we kinda diverted there for a bit haha…
I believe the issue with the form not being visible is actually correct, and expected as well. When Angular creates the isolated scope for the directive, because it is using ng-transclude, it will actually create a new child scope for the transcluded content. This new scope does not prototypically inherit from it’s parent, thus it’s not available on $scope.
One way to get around this is to use $parent. This will tell Angular to look to the parent scope for the property userForm: