From 5d80d32082cee201f528f774aa2b92ba2ef97542 Mon Sep 17 00:00:00 2001 From: Tyler Morgan Date: Tue, 23 Dec 2014 18:33:03 -0800 Subject: [PATCH] docs(guide/Directives): data from isolate to parent scope It looks like this used to be in the Angular docs as per this thread: https://groups.google.com/d/msg/angular/3CHdR_THaNw/AxqKwUw5t0oJ I recently spent some time trying to get this to work and was very frustrated by lack of documentation. I tried to tie this into the existing &attr example but let me know if that is not desirable. --- docs/content/guide/directive.ngdoc | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/docs/content/guide/directive.ngdoc b/docs/content/guide/directive.ngdoc index a82e6994e20f..6e81d00e106c 100644 --- a/docs/content/guide/directive.ngdoc +++ b/docs/content/guide/directive.ngdoc @@ -751,9 +751,12 @@ own behavior to it. angular.module('docsIsoFnBindExample', []) .controller('Controller', ['$scope', '$timeout', function($scope, $timeout) { $scope.name = 'Tobias'; - $scope.hideDialog = function () { + $scope.message = ''; + $scope.hideDialog = function (message) { + $scope.message = message; $scope.dialogIsHidden = true; $timeout(function () { + $scope.message = ''; $scope.dialogIsHidden = false; }, 2000); }; @@ -771,6 +774,7 @@ own behavior to it.
+ {{message}} Check out the contents, {{name}}! @@ -778,7 +782,7 @@ own behavior to it.
- × + ×
@@ -796,7 +800,10 @@ callback functions to directive behaviors. When the user clicks the `x` in the dialog, the directive's `close` function is called, thanks to `ng-click.` This call to `close` on the isolated scope actually evaluates the expression `hideDialog()` in the context of the original scope, thus running `Controller`'s `hideDialog` -function. +function. Often it's desirable to pass data from the isolate scope via an expression and to the +parent scope, this can be done by passing a map of local variable names and values into the expression +wrapper fn. For example, the hideDialog function takes a message to display when the dialog is hidden. +This is specified in the directive by calling `close({message: 'closing for now'})`.
**Best Practice:** use `&attr` in the `scope` option when you want your directive