1

In the javascript file, the code is like this:

angular.module("amodule")
  .directive("bdirective",function(){
    return{
      scope: {
        testIsolated: '&test',
      },
      restrict:'EA',
      replace: true,
      templateUrl: "address.html",
    }
  .controller{"ccontroller",function($scope){

    $scope.test=function(){
                           w="changed"
                           return w;
                          };
    };

In the index.html file, the code is like this:

<bdirective testIsolated="test()"></bdirective>
...

In the address.html file, the code is like this:

<button ng-click="result=testIsolated()">Button1</button>
...

I think when the Button1 is clicked, testIsolated should run, and then test() should run. The value of "result" should be changed to "changed".

But it doesn't work

Can anyone help me figure it out?

Thanks in advance

1
  • Hello. This seems like a useful question but the language is a little vague - it's hard to pick up at a glance what issue you are having, could you tweak this question to focus on the issue, maybe add a bold one line statement outlining what you'd like clear up Commented Aug 20, 2015 at 23:32

1 Answer 1

1

It would have worked if you:

#1 either changed the attribute to test:

<bdirective test="test()"></bdirective>

Or, #2 changed how your isolate scope binding was defined:

scope: {
   testIsolated: "&"
}

and the attribute was hyphenated to test-isolated due to how Angular normalizes attributes:

<bdirective test-isolated="test()"></bdirective>
Sign up to request clarification or add additional context in comments.

6 Comments

Thx. how should I call the test() method in address.html?
@Bluesea, exactly like you do right now... you just have a mismatch between between the attribute of bdirective and the bound scope variable
@Bluesea, here's a demo with approach #1: plnkr.co/edit/IN3XbWtRPFRZf1fKZDli?p=preview. It's the same as your code with minor fixes
I would really appreciate it if you can make test() have an argument in the Demo. For example: $scope.test=function(date){ var w="Today is"+date; return w; }.
@Bluesea, this is really a different question - a question about how to pass local variables into the bound expressions - and should have been asked separately. Anyway, this is how it can be done: plnkr.co/edit/OnRED9iekq9g0lYd2kps?p=preview
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.