DEMO
Consider the following directive:
angular.module('MyApp').directive('maybeLink', function() { return { replace: true, scope: { maybeLink: '=', maybeLinkText: '=' }, template: '<span>' + ' <span ng-hide="maybeLink" ng-bind-html="text"></span>' + ' <a ng-show="maybeLink" href="#" ng-bind-html="text"></a>' + '</span>', controller: function($scope) { $scope.text = $scope.maybeLinkText.replace(/\n/g, '<br>'); } }; });
The directive adds both the <span>
and the <a>
to the DOM (only one is visible at a time).
How could I rewrite the directive such that it will add either <span>
or <a>
to the DOM, but not both?
UPDATE
OK, I guess I could use ng-if
like that:
template: '<span>' + ' <span ng-if="!maybeLink" ng-bind-html="text"></span>' + ' <a ng-if="maybeLink" href="#" ng-bind-html="text"></a>' + '</span>'
But, how could one get rid of the surrounding <span>
in this case?
UPDATE 2
Here is a version of the directive that uses $compile
. It doesn't have the surrounding <span>
, but the two way data binding doesn't work either. I'm really interested to know how to fix the two way data binding issue. Any ideas?
DEMO
angular.module('MyApp').directive('maybeLink', function($compile) { return { scope: { maybeLink: '=', maybeLinkText: '=' }, link: function(scope, element, attrs) { scope.text = scope.maybeLinkText.replace(/\n/g, '<br>'); if (scope.maybeLink) { element.replaceWith($compile('<a href="#" ng-bind-html="text"></a>')(scope)); } else { element.replaceWith($compile('<span ng-bind-html="text"></span>')(scope)); } } }; });
Currently, there is NO way to conditionally apply a directive to a component. This is not supported. The components which you have created can be added or removed conditionally. There is already an issue created for the same with angular2 , so it should be the case with angular4 aswell.
Components are directives with templates. The only difference between Components and the other two types of directives is the Template. Attribute and Structural Directives don't have Templates. So, we can say that the Component is a cleaner version of the Directive with a template, which is easier to use.
Answer: C is the correct option. The ng-app directive is used to initialize the AngularJS application. The ng-init directive is used to initialize the application data.
You might be able to use a template
function. According to the docs:
You can specify template as a string representing the template or as a function which takes two arguments tElement and tAttrs (described in the compile function api below) and returns a string value representing the template.
function resolveTemplate(tElement, tAttrs) { } angular.module('MyApp').directive('maybeLink', function() { return { //... template: resolveTemplate, //... }; });
I think this is the cleanest way to inject a dynamic template based on a scope property
angular.module('app') .directive('dynamic-template', function () { return { template:'<ng-include src="template"/>', restrict: 'E', link: function postLink(scope) { scope.template = 'views/dynamic-'+scope.type+'.html'; } }; })
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With