I have a simple auto-complete dropdown. It works fine when I use only controller.
I want to Make it reusable,so I made a custom directive with isolated scope.
But the problem is when I type on search text box, it does not call the controller function which is assigned to ng-change
instantly. when I start typing once again, it calls the function. Again it takes the value what I typed before not the current model value.
I am new to custom directives... I really don't know how to update model instantly and pass from directive scope to controller scope.
Another think I can't able to pass the function parameter from html to controller function.
I think I have to use $apply
or $digest
somewhere.. But where and how should I use ?
autofillApp.directive('autofillDropdown', function($rootScope) {
return {
restrict: 'A',
templateUrl: "dropdowntemplate.html",
replace: true,
scope: {
'items': '=autofillItems',
'selected': '=autofillSelected',
'change': '&autofillChange',
'focused': '=autofillFocus',
'onSelect': '&autofillOnselect'
},
link: function(scope, element, attr) {
//console.log(scope.$$watchers);
//console.log(element);
//console.log(attr);
return
},
compile: function(element, attributes) {
var linkFunction = function($scope, element, attributes) {
$scope.$apply();
}
return linkFunction;
}
};
})
Here is My Plunkr : Plunkr