When having a variable input type checkbox the value is not set correctly.
This code is simplified in order to get the problem across.
controller.coffee
angular.module 'tinyhandsAmenApp'
.controller 'TestrouteCtrl', ($scope) ->
@user =
admin: true
directive.coffee
angular.module 'tinyhandsAmenApp'
.directive 'formInput', ->
restrict: 'EA'
scope:
model: '='
template: '<input ng-model="model" type="{{ type }}"/>'
link: (scope, element, attrs) ->
scope.type = "checkbox"
example.html
<div class="col-md-12" ng-controller="TestrouteCtrl as tr">
<input type="checkbox" ng-model="tr.user.admin" />
<form-input model="tr.user.admin"></form-input>
</div>
The resultant html has value="true"
which makes it seem that Angluar is seeing it as a textbox. If I replace {{ type }}
with checkbox
then it works. Both the input in the directive and out are set correctly.
I also found out if I change the directive html to be
<form-input model="tr.user.admin" type="checkbox"></form-input>
and the scope object in the directive js to be
scope:
model: '='
type: '@'
and remove the scope.type = "checkbox"
from the link function it works!
So it is seeming like Angluar is setting the value of the input element with ng-model BEFORE link is being called. Does that make sense?
I suppose this solution is good enough for now but I would REALLY like to be able to specify the input type from the js.
By the way, the way I had it originally works fine if type is text.
UPDATE: Here is a JSFiddle that shows what I am talking about. form-input
doesn't work. form-input2
does but using the type attribute on the directive in the html.
UPDATE2: What I want to be able to do is specify the input type in the javascript and have the binding to the controller work.