A JSFiddle is here:
The problem description is as follows, I have a controller wrapping a form which contains some logic, and can not be defined within the directive hash as controller:
I need the ability to populate a field from a directive dynamically like so:
App.Directive.SomeAwesomeDirective = ->
restrict: 'C'
link: (scope, element, attrs) ->
someValue = scope.someValue
field = element.find(".some-class")
scope.fieldValue = someValue
field.ngModel = "scope.fieldValue"
field.ngClass = "scope.someAwesomeClass"
monitorFields = (newValue, oldValue) ->
console.log "we are here"
console.debug newValue
console.debug oldValue
scope.addValue(newValue)
scope.$watch "scope.fieldValue", monitorFields, true
I need the following to be fulfilled:
1) When the textfields value is changed, I want scope.fieldValue to be updated.
2) After this happens, I want the addValue method (defined on the wrapping controller), to be called with the new value for validation.
3) The addValue method should set the someAwesomeClass scope variable, and the input fields class should update.
4) The ngClasses to be applied are ng-valid/ng-invalid. Form validation should function correctly in correspondence with these classes
As can be seen in my jsfiddle, none of these things are currently happening, and I am unsure as to why…
Thanks!
You can fix it by defining a directive
someClass, which will execute the function on its parent. Theformtag gets an extra attributeexecute, which holds the function to execute. ThesomeClassdirective will search the controller ofdirdirective (hence require: ‘^dir’) and execute it.Another solution would have been to drop the
dirdirective and define theexecuteattribute on thesomeClassdirective (e.g. when each input field should trigger a different function).Directives:
See jsFiddle.