controller and template are the standard AngularJS components required to render an extension using AngularJS. Both have already been comprehensively explained in Chapter 11, Coding in Qlik Sense.
When using AngularJS, the basic structure of your code compared to the paint approach looks as follows:
define(['text!./template.html'],
function(template) {
return {
paint: function(){},
resize: function(){},
template: template,
controller: ['$scope', '$element', function (scope, $element) {
console.log("This is Mastering Qlik Sense");
}
};
});
The equivalent of layout in the paint() method can be found in $scope.layout object.
Loading Resources
When developing extensions within Qlik Sense, you can reference many already existing JavaScript libraries that are being loaded with the Qlik Sense AngularJS module and are presently available in your scope. You can load these resources at the top of your extension code, using the following approach:
define( [ /* dependencies */ ],
function ( /* returned dependencies as arguments */ )
{ ... }
);
For example, if you wish to include jQuery or d3 in your extension, all you need to do is reference them in the definition part. Extending the previous code sample, it would look as follows:
define(['text!./template.html', 'jQuery', 'd3'],
function(template, $, d3) {
return {
paint: function(){},
resize: function(){},
template: template,
controller: ['$scope', '$element', function (scope, $element) {
$element.html("This is Mastering Qlik Sense");
}
};
});
This would load the jQuery and d3 modules, which can then later be referenced in the code using the $ and d3 variables.
Furthermore, you can use RequireJS to load many more resources of different kinds, such as the following:
- Style sheets / CSS files
- JavaScript libraries (see the previous example)
- Images
- Fonts
- Items from the content library
The most relevant local resources that can be loaded straightaway are the following:
- jQuery
- d3v4
- Qlik (to use the Engine API)
- AngularJS