I've been playing around with angular trying to understand how it manages scopes, then I found that I couldn't update the variables in the directive using a function call.
To illustrate the issue, here is my simple app: The idea is that when you click the toggle link, the menu should show up and when you click it again or somewhere else, the menu should disappear.
angular.module('app', [])
.controller('DemoController', ['$scope', function($scope) {
}])
.directive('dropdown', function() {
return {
restrict: 'E',
transclude: true,
controller: function($scope) {
$scope.onBlur = function () {
// this doesn't actually work
$scope.showMenu = false;
};
},
};
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<body id="body" ng-app="app" ng-controller="DemoController">
<dropdown ng-transclude>
<a href="#" ng-click="showMenu = !showMenu" ng-blur="onBlur()">Toggle</a>
<menu ng-show="showMenu">
<div>I'm</div>
<div>the drop-down</div>
<div>menu</div>
</menu>
</dropdown>
</body>
Here's my plunker links:
Non-working version: http://plnkr.co/edit/rfdt5FEoGAOX15RZUsKA?p=preview
Working version: http://plnkr.co/edit/xMANGDVa8n64OKK3gOgg?p=preview
The difference between the working one and the other is that the working version simply uses ng-blur="showMenu = false"
instead of calling the function. If I call $scope.$apply()
inside the controller function, then I will get "$apply already in progress" exception.
I guess I must be missing something here but I've no idea why right now. Thanks in advance.