2

From the Angular docs:

& or &attr - provides a way to execute an expression in the context of the parent scope. If no attr name is specified then the attribute name is assumed to be the same as the local name. Given and widget definition of scope: { localFn:'&myAttr' }, then isolate scope property localFn will point to a function wrapper for the count = count + value expression.

This gives me the impression that something like this should be possible, but it isn't working. What am I doing wrong?

myApp.directive('test', function () {
return {
    restrict: 'E',
    replace: true,
    template: '<div><input type="button" ng-click="thefunc()" value="{{title}}"/></div>',
    scope: {
        title: '@',
        thefunc: '&'
    }
};});

HTML:

<test thefunc="alert('Here you go.')" title="Click me for a popup"/>

Fiddle

1 Answer 1

3

Angular is looking for an "alert" on the controller's scope -

$scope.alert = function(){..}

and not the window.alert

Here is an updated version:

http://jsfiddle.net/3CxtZ/4/

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks. I guess I was thinking it just eval()s the contents of the &attribute or something, but clearly that's wrong. It's easy to forget how isolated these scopes really are.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.