I'm trying to implement a directive that would delay the apparition of a container using ng-show
and a $timeout
.
Here's what my directive looks like:
angular.module('myApp')
.directive('delay', function($timeout) {
return {
template: '<div ng-show="showIt" ng-transclude></div>',
replace: false,
transclude: true,
scope:true,
restrict: 'A',
link: function postLink(scope, element, attrs) {
$timeout(function() {
scope.showIt = true;
}, attrs.delay);
}
};
});
Then, I would use it in my view like this
<div delay="1000">
<intput type="text" ng-model="myText"/>
</div>
So far, the delay works. Yeah, I'm proud. But then, myText
isn't accessible anymore from the controller because it's not visible by the parent scope. I tried changing the scope to this instead:
scope: {
myText: '='
}
to establish a two-way data-binding...without any success.
What would be the simplest way to implement what I'm trying to achieve using a directive? Thanks a lot!
EDIT: The golden rule
Thanks a lot to GregL for his answer!
The best way around was simply to wrap my ng-models in an object to make use of dot notation to avoid binding the ng-model to the child-scope. Child scope use prototypal inheritance to look up its value, so when the value has been set in the child scope, then it no longer looks up the parent scope.