<graph tag='tag_identifier'> <nvd3 options='hist.options' data='hist.data' api='hist.api' ...> </graph>
The 'tag' attribute is an identifier for the thing being graphed (it will be fetched using ngResource) and 'api' is a reference to the nvd3 api. I will need this to call methods, such as refresh(), inside the nvd3 directive, so I have to pass these through. Inside nvd3, the scope is declared like this:
scope: { data: '=', options: '=', api: '=?', events: '=?', config: '=?' }but I don't want data, options, and api bound to my scope, I want them bound to my controller. The answer is described pretty well by Pascal Precht in his post Exploring Angular 1.3: Binding to Directive Controllers. This works equally well in Angular 1.4.7. I modified my registration function as follows:
static register(module) { module.directive('graph', [ ()=> { return { restrict: 'AE', transclude: false, replace: true, controller: GraphDirectiveController, controllerAs: 'hist', scope: {}, /* true, false, or isolate */ bindToController: { 'tag': '@tag', 'api': '=api' }, templateUrl: "views/graph_widget.html", link: function (scope, elem, attrs, ctrl) { /* set 'options' and 'data': */ ctrl.initGraphOptions(); } }; }]); }When it comes to the lifecycle, three things are going to happen:
- Instantiation - first, angular will create an instance of your controller
- Setters - next, it will bind what used to be a scope to your controller instance
- link() - this is your chance to do any post-initialization after bindings are set up
set api(_api) { console.log("Setting nvd3 API"); this._api = _api; }
No comments:
Post a Comment