If I understand correctly, you want to set the $scope.parent object that appears in the text box based on $scope.ticketCategory.parentId
If so, then directive is not the best way to do this. The directive is designed to manipulate the DOM or logic that affects your opinion. But this function has nothing to do with updating the view. It just happens that the view is attached to $scope.parent , but if you just install this object in the code, the view will automatically update (or maybe with a call to $apply() )
In this case, you must create a service or filter to make this operation reusable (also be sure to return as soon as a match is found, since there is no need to continue the cycle after that, right?)
However , if you insist on using the directive. You can create a directive that depends on the ng model and set the model value based on other attributes (list of categories, selected identifier). So the usage will be like that, perhaps
<input type="text" ng-model="parent" set-model="ticketCategory.parentId" set-model-list="queryTicketCategories" >
and the directive code will basically find the object from queryTicketCategories , which has the same identifier as ticketCategory.parentId , and set the model with this object.
You should probably set a flag or so in the directive to run this code only once, and not every $apply() loop. But with the service / filter approach, you can set the parent value whenever you want (perhaps after a server request or in a callback), so I recommend that you use this instead of the directive.
Gaafar
source share