I created a directive that accepts some attributes and initializes an isolated area with these attributes. If the attribute is not specified, then the isolated area must be initialized with the calculated value.
I added a communication function that checks the scope and initializes the default values ββ(if the value has not been set using the attributes). The volume was initialized, but if I set the default value, it will be overwritten later by the framework.
A workaround is to use $ timeout (...) and install it later, but it seems too big due to a hack.
function ($timeout) { return { scope: { msg1: '@', msg2: '@' }, template: '<div>{{msg1}} {{msg2}} {{msg3}}</div>', link: function ($scope, $elt, $attr) { var action = function() { if (!$scope.msg2) $scope.msg1 = 'msg1'; if (!$scope.msg2) $scope.msg2 = 'msg2'; if (!$scope.msg3) $scope.msg3 = 'msg3'; }; action();
I prepared a JSFiddle to illustrate what happens.
- msg1 is initialized through an attribute and always has the correct value.
- msg2 is not initialized with an attribute, but can be specified using an attribute. This value is overwritten after the link method is called.
- msg3 is not initialized via an attribute, and this is not even possible. This value is set when the controller is built and works fine.
It seems that AngularJS creates the scope and updates its value after the controller is created, and the directive is associated with the DOM. Can someone tell me the recommended way to do this?
source share