0

I have a flag I'm setting to true/false that changes the way some items are being displayed on the page. I'm running into some trouble getting the value into my template and having it actually work though. The normal HTML (not inside the template) works fine.

restrict: 'E',
        scope: {
            speedDirection: "@",
            speedName: "@",
            value: "@",
            editElements: "="
        },
        template:
        '<div>'+
          '<p class="body-text">{{speedDirection}} '+
          '<input type="text" name="{{speedName}}" value="{{value}}" ng-show="editElements">'+
          '<span ng-hide="editElements">{{value}}</span>'+
          '</p>'+
        '</div>',

I would like to use the dynamic model value of editElements though, and not just the passed through value. That way when it changed it would reflect the update in the displayed template. The variable is defined in an object where I'm storing my test data:

function MyObject($scope) {
    $scope.editElements = true;

Is there any way to get this to work? I've tried passing it through (as above), using the "@" and {{editElement}} bit and so on. The custom HTML portion:

<speed-limit speed-direction="A to B:"
                    speed-name="reverse"
                    value="{{newObject.speedLimit[0]}}"
                    editElement="editElements">
                    </speed-limit>

UPDATE: I've installed Batarang and it shows the specific portion of my custom element (speed-limit) to have "editElement: null". However the other areas editElements is referenced are correctly set. Is this some weird scope problem?

7
  • Your ng-show/hides are using editElements, which is different from what is defined in the scope: {...}, which is editElement. Do you want the directive to use a different name or the same? Commented Feb 6, 2013 at 17:26
  • If I didn't have to pass the editElements in through HTML like I'm doing now, then I'm ok with keeping the same name. Commented Feb 6, 2013 at 17:33
  • You can use the same name for the attribute, but you do need to pass it to the directive via HTML since you are creating an isolate scope. Try this: in the HTML, editElements="editElements"; in the directive: scope: { ..., editElements: "="}. Leave your template as is. Commented Feb 6, 2013 at 17:37
  • According to Chrome's developer tool, the value isn't being interpreted. It's just showing up as ng-hide="editElements" rather than ng-hide="true" Commented Feb 6, 2013 at 18:22
  • Ok, adding more details to the problem now that I've inspected with Batarang. Commented Feb 6, 2013 at 18:28

1 Answer 1

2

As discussed in the comments, the same name can be used for the attribute's name, but it must be snake-case, hence edit-elements="editElements":

<speed-limit speed-direction="A to B:"
     speed-name="reverse"
     value="{{newObject.speedLimit[0]}}"
     edit-elements="editElements">
</speed-limit>

Don't feel bad about this... I think every Angular developer has wasted at least one hour (or more) of his/her life on this.

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

1 Comment

I submitted an issue on github, this isn't my first time running into this embarrassingly enough. I DO think Angular should throw SOME type of error. At the very least a "no such variable found" one.

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.