I have a custom directive that draws a form like this:
app.directive('customForm', function() {
return {
restrict: 'E',
scope : {
data : "="
} ,
replace: true,
controller: ['$scope', '$element', function($scope, $element) {
............
................
$scope.submit=function(){
if($scope.formName.$error) {
return
}
}
........................
}],
,templateUrl: function(element, attr) {
return '/views/directives/custom-form.html';
}
Where custom-form.html
has a form named formName
, my issue is that when I access the form object form the html itself it's working, but in the directive controller it's undefined
html:
<div>
formName : {{formName}}
<!-- here the object is printed correctly with all of its properties ,
and when i insert a wrong value it correctly becomes $invalid true -->
<form novalidate name="formName" id="{{data.active.label}}-container">
..........
....................
<button type="button" ng-click="submit()"> Submit</button>
</div>
while the check in the controller:
if($scope.formName.$error) {
return
}
hits an error of
can't read $error of undefined
What is going on exactly? How come it's binded on the view but not on controller?