I have been developing an AngularJS Directive which is supposed to work as a stand alone widget which can be configured by setting attributes in the form of Objects. My Primary Objectives are below:
- Do the processing inside directive based on the Object passed in attributes. the Processing does involve fetching REST API response. SO I am using controllers to achieve that.
- After My Directive does some processing, It may change the Object which my Parent Controller should be able to change.
Below is the sample code which covers up my objective.
Directive Usage:
<post-listing page-id="currentPage.id" config="postConfig" posts="posts" />
Code for Directive
'use strict';
angular.module('myApp')
.directive('postListing', function () {
return {
templateUrl: '/views/directive.post-listing.html',
restrict: 'EA',
scope: {
page_id:"=",
config:"=",
posts:"=",
},
controller: "DirectivePostListingCtrl"
};
});
Code for Directive Controller:
'use strict';
angular.module('myApp')
.controller('DirectivePostListingCtrl', function ($scope, $rootScope, $routeParams)
{
// Here I want to access the page_id and config as the actual value
// Here I will also update the posts array which is exposed to outside of directive.
});
Code for template:
<h4>Displaying Posts for {{page.title}}</h4>
<ul>
<li ng-repeat="p in posts" >{{p.title}}</li>
</ul>
When I run this code, it says $scope.page_id either "undefined" or it says "currentPage.id" (based on the operator selected = or @ ) where I expect it to be the value of currentPage.id .
Please suggest.
Thanks in advance.