I am new to angular and am struggling to pass a variable that I have retrieved in my controller over a http resource into my directive. Firstly I have the issue that my ngResource call is asynchronous and then I have an added issue that my resource call is chained.
This is my html
<html ng-app="routingRulesApp">
<body ng-controller="RulesDisplayCtrl">
<my-customer info="$activeRuleSetId"></my-customer>
</body>
</html>
This is my Javascript
var routingRulesApp = angular.module('routingRulesApp', [
'routingRulesControllers',
'routingRulesServices',
'routingRulesDirectives'
]);
var routingRulesControllers = angular.module('routingRulesControllers', []);
routingRulesControllers.controller('RulesDisplayCtrl', ['$scope', 'RuleSets', '$q',
function($scope, RuleSets, $q) {
var fr = null;
var rpromise = $q.defer();
$scope.activeRuleSetId = RuleSets.active({ruleId: 1}, function(activeRuleSetId) {
var ruleSetId = activeRuleSetId[0];
var ruleSet = RuleSets.query({ruleSetId: ruleSetId}, function(ruleSet) {
console.log(ruleSet);
fr = ruleSet;
rpromise.resolve(fr);
}, function(response) {
//404 or bad
if(response.status === 404) {
console.log("HTTP Error", response.status);
}
});
}, function(response) {
//404 or bad
if(response.status === 404) {
console.log("HTTP Error", response.status);
}
});
$scope.formattedResults = rpromise.promise;
}
]);
var routingRulesDirectives = angular.module('routingRulesDirectives', []);
routingRulesDirectives.directive('myCustomer', [
function() {
return {
restrict: 'E',
replace: true,
scope: {
formattedResults: '=info'
},
templateUrl: 'templates/currency-group-rule.html',
controller: function($scope) {
console.log($scope.formattedResults);
debugger;
}
};
}
]);
var routingRulesServices = angular.module('routingRulesServices', ['ngResource']);
routingRulesServices.factory('RuleSets', ['$resource',
function($resource){
return $resource('data/routing-ruleset-:ruleSetId.json', {}, {
query: {method:'GET', isArray:true},
active: {method:'GET', isArray: false, url: 'data/rulesets-activeRuleSetId.json', responseType:"text"}
});
}]);
I am trying to get hold of my $scope.formattedResults variable inside my directive controller so that I can build a custom table / grid solution but I am unsure of how to achieve this. As you can see I am very lost. I have attempted to use deferred objects and hoped that it would bind to a variable.