I'm having trouble understanding how to define functions to be used by (or inside of) a directive that has an isolate scope. In the following code, why doesn't the $scope.foo() function execute? Is there a correct way that I should be approaching this problem? I'm looking to make a button that is visible only if a user is logged in, and was thinking that a directive would be a nice way to encapsulate/isolate that functionality.
<!DOCTYPE html>
<html>
<head>
<script src="https://code.angularjs.org/1.4.2/angular.js"></script>
<script>
angular.module('MyApp', [])
.directive('myScopedDirective', function() {
return {
scope: {}, // <!-- isolate scope breaks things
controller: function($scope) {
// isolate scope prevents this function from being executed
$scope.foo = function() {
alert('myScopedDirective / foo()');
};
}
};
});
</script>
</head>
<body>
<div ng-app="MyApp">
<!-- button doesn't work unless isolate scope is removed -->
<button my-scoped-directive ng-click="foo()">directive container - foo()</button>
</div>
</body>
</html>