angularjs - Why is FormController expecting a name in my form to inject itself? -
i noticed if form has name attribute this:
<form name="myform"></form>
then can hold of formcontroller this:
var form = $('form')[0]; var formcontroller = $(form).scope().myform;
what deal pattern? why does't angular put "formcontroller" in form's scope - common when implementing custom directives?
i curious understand if pattern should expect in other cases , if should follow while implementing own directives.
thanks
the documentation mention in comments state when using name attribute, formcontroller published onto current scope. means access controller need $scope.myformsname
.
<!doctype html> <html ng-app="myapp"> <head> <script data-require="angular.js@1.5.9" data-semver="1.5.9" src="https://code.angularjs.org/1.5.8/angular.js"></script> <link rel="stylesheet" href="style.css" /> <script src="script.js"></script> </head> <body ng-controller="myctrl"> <h1>hello plunker!</h1> <form name="myform"> <input name="birthdate" type="text" /> </form> {{ myform | json }} </body> </html>
and in controller:
angular.module('myapp', []).controller('myctrl', ['$scope', function($scope){ $scope.myform; // here's formcontroller }])
when implementing own directives approach different. should use require
property of directives, explained in depth here. property, can tell angular directive requires (can optional requirement) said controller, , provide in link function:
mod.directive('crudform', ['crudtemplatesdir', function (tpldir) { return { scope: { fields: '=*', record: '=', listsofvalues: '=*?' }, require: '^^form', // can string 1 or array of strings multiple requirements restrict: 'e', templateurl: tpldir + 'form.tpl.html', link: function(scope, elem, attr, requiredcontroller){ // required controller 1 defined in 'require' string. if defined array, array of controllers } }; }]);
Comments
Post a Comment