So, I am trying to make generic directive that will take in scope variable
from controller to change and function to execute when its changed.
<div my-directive selected-user='user.id' refresh='refresh()'></div>
'use strict';
function() {
return {
restrict: 'A',
replace: true,
scope: {
selectedUser: '=selectedUser',
refreshFunction: '&refresh'
},
templateUrl: 'common/directives/user/user.html',
link: function(scope) {
// we have here function that builds a list and on click of the
element selctedUser is changed and update is ran
// changes the current selected member
scope.updateSelected = function(data) {
scope.selectedUser = data.id;
scope.refreshFunction();
};
loadMembers();
}
}
);
So directive properly updated the id and goes to run function inside the
controller that is passed, lests say it looks like this:
$scope.refresh = function() {
// here the value of user.id is not yet updated from directive and it
uses the old value to make service call
SomeService.get(user.id).then(function() {
// here user.id is an expected changed value from inside directive
});
}
I guess there is a problem with scope not having the time to actually make
the change of the variable cause function is immediately called inside
directive and
controller scope has the old value yet.
I mean I made it work with using watch in controller or sending argument to
that function, but want to really understand why is this happening and how
can it be done properly cause sending in function inside this directive
would be much nicer.
Is there anything that can be done to give the cycle time to update scope
before calling function if that is what is actually happening. I'm still in
learning angular so forgive me if there is anything stupid in this
question, but I want to know why exactly is this happening.
Thanks
--
You received this message because you are subscribed to the Google Groups
"AngularJS" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/angular.
For more options, visit https://groups.google.com/d/optout.