ngShow in directive with isolate scope is never shown
In a directive with an isolate scope with ng-show="someVar" (someVar from a parent ng-controller's $scope), the directive's tag is never shown ("display: none" isn't removed) (example). Other directives (like ng-repeat) bind values correctly in custom directives with isolate scope.
I'm wondering if simply providing a way to reference to the parent scope (even though it technically isn't a parent) would help. $outerScope or $externalScope or something to indicate it isn't a parent in the inheritance sense.
@chrisnicola something like $parent.foo ?
+1
This one keeps driving me nuts - I keep running into this and the only way to fix it is to add another div or make the scope not-isolated.
Adding the following into the link function solves the problem. It's an extra step for the component creator, but makes the component more intuitive.
function link($scope, $element, attr, ctrl) {
if (attr.hasOwnProperty("ngShow")) {
$scope.$watch("ngShow", function (value) {
if (value) {
$element.show();
}
else {
$element.hide();
}
});
}
You'll also need to setup scope bindings for ngShow
scope: {
ngShow: "="
}
Other languages use OOP inheritance to solve issues like this, for example, if you had BaseComponent and that had a property called show, then in CustomComponent, which extends BaseComponent, show would already exist. And... you could set the value of show from an external "scope". Maybe directives with isolate scopes should automatically inherit the "ng" attributes unless otherwise specified in the directive. That's how I might solve this problem.
OH yeah and... +1 ;)
and another +1
Would it be more elegant to have another type of scope declaration that would allow access to isolate scope it it already exists or original scope if it doesn't?
app.directive('MyDir', function(){ return { scope: 'inherit' //This would grab any scope available on the element already }; });