Thursday 15 March 2012

angularjs - Accessing 'hidden' directive scope -


I'm relatively new to Angular's instructions and I'm trying to understand how it works. I think I understand it well but then I go into a problem like the following: My instructions:

  & lt; Ng-view & gt; & Lt; Div my-directive ng-click = "foo ()" & gt; ... & lt; / Div & gt; & Lt; / Ng-view & gt;   

Director code:

  app.directive ('myDirective', function) {return work (area, element, ethers) {scope.foo = function () {...}}}   

When I was clicking on the element, foo () was expecting the fire but it is not. Scratch Scope with Batarang I see that the scope exists (id: # 003) as its parent with rootScope, but when the element is clicked on the element or its children (id: # 002), then I checked with angular also .element ($ ('div [my-directive]')). Scope () Thinking this could be a bold issue but the result was the same.

Update: I think I found the reason for the problem, the element using the instruction in my actual app is inside the code ngView When I put my instructions outside of NgView, the properties defined in the link function are now visible in the scope (root scope). I think this is due to the async load, but I need to confirm this, I used ngView with a static template but it does not work (it works, I do not reproduce the problem Could).

One way to achieve this is to use a template and closing in your instructions:

  app.directive ('my directive', function () {return {template: 
$ ethers) {$ scope.foo = function () {...}}}}}});

And your HTML:

  & lt; Div my-directive & gt; ... & lt; Div & gt; The reason for this is that you direct the  foo  function to   in the scope. In your original effort,  foo  is likely to be in the parent area. Although it is possible to change the parent's scope for your instructions, it is not possible that what you want is the internally defined code of  foo  In order to know about the function, its instructions are asking the user that this is a bit weird / backward   

No comments:

Post a Comment