I changed sections (div
) of my html to directives, before changing that, my html looks like this:
<div ng-controller="SearchCtrl as search">
<div id="firstDirective">
.
.
.
<div>
<div id="secondDirective">
.
.
.
<div>
</div>
which works fine with the associated controller. I plan on changing the html to this:
<div ng-controller="SearchCtrl as search">
<first-directive>
<second-directive>
</div>
The corresponding angular.directive
definitions have isolated scopes (return {..., scope: {attr: '=attr', func: '&func'},...}
) and attributes and functions are passed from the controller.
After changing the first div
to a directive (first-directive
), it is still working well. However, after creating the second-directive
and replacing the corresponding div, that section does not work anymore (the first still works). Here is my controller:
app.controller('SearchCtrl', ['$scope', '$http', function($scope, $http){
var self = this;
self.data = {};
self.clickSearch = function() {
self.retrieve(param1, param2);
.
.
.
console.log('clickSearch log, data: ', self.data);
}
// retrieve is reused, called directly in some parts of html,
// thus I attached it as a controller function
self.retrieve = function(param1, param2) {
// some $http.get and assign to data
.
.
.
console.log('retrieve log, data:', self.data);
}
// some other functions, some are passed to the directives
.
.
.
});
However the logs show:
[LOG]clickSearch log, data: {}
[LOG]retrieve log, data: {...//some assigned values}
It looks like that clickSearch function finishes first, then retrieve
function executes later, thus I get empty data. My background is from Java, so I do not fully understand the concept of callback in JavaScript, and I suspect that's what's happening here. How could I remedy this? Thanks.