javascript - How to access form object with two-way-binding in angular directive without isolated scope -
as titled, given following simple example:
<form name="myform"> <button my-directive-test="myform">hello</button> </form> app.directive('my-directive-test', function() { return { restrict:'a', link: function(scope, element, attrs) { // how scope.myform.$submitted, scope.myform.$errors without isolated scope? }) }; });
currently have been doing is:
<form name="myform"> <button my-directive-test="{{myform.$submitted}}">hello</button> </form> app.directive('my-directive-test', function() { return { restrict:'a', link: function(scope, element, attrs) { scope.formsubmitted = scope.$eval(attrs.mydirectivetest); if (scope.formsubmitted) { } else { } }) }; });
but achieve is:
<form name="myform"> <button my-directive-test="myform">hello</button> </form> app.directive('my-directive-test', function() { return { restrict:'a', link: function(scope, element, attrs) { if (scope.myform.$submitted) { } else { } }) }; });
but far haven't found way bind form object in directive.
when not using isolated scope, directive scope same "parent" element's scope. work:
<form name="myform"> <button my-directive-test="myform">hello</button> </form> app.directive('mydirectivetest', function() { return { restrict:'a', link: function(scope, element, attrs) { if (scope[attrs.mydirectivetest].$submitted) { } else { } }) }; });
Comments
Post a Comment