1

I'm trying to make input wrapper with angular directives and it should have different labels. Now I even can't access model outside the input (even with scope: false).

HTML:

<input-block data-label="my label">
    <input class="input-field" type="text" name="test" ng-model="test"/>
</input-block>

Test: {{test}} <!--not working-->

Angular:

profileApp.directive('inputBlock', function() {
    return {
        replace: true,
        restrict: 'E',
        transclude: true,
        template: '' +
        '<div class="input-block">' +
            '<span class="input-text">{{label}}</span>' +
            '<ng-transclude></ng-transclude>' +
        '</div>',
        link: function(scope, element, attrs) {
            scope.label = attrs.label;
        }
    };
});

The only idea for now is to find a way to isolate single variable or something similar

8
  • 1
    always always always use an object in ng-model ! Commented Oct 16, 2016 at 22:11
  • @charlietfl that doesn't sound right. What do you do for <input type="text">? Commented Oct 16, 2016 at 22:14
  • not sure what you mean @Phil Commented Oct 16, 2016 at 22:17
  • 1
    @Phil am talking about the golden dot in ng-model rule Commented Oct 16, 2016 at 22:38
  • 1
    @Phil strings are primitives in Javascript, not objects (as they are in many other languages). This can cause some unexpected behavior if one is not aware. Commented Oct 17, 2016 at 0:12

1 Answer 1

2

as was already said here, just use object for your ng-model:

<body ng-init="model = {}">
  <input-block data-label="my label2">
    <input class="input-field" type="text" name="test" ng-model="model.test"/>
  </input-block>

  Test: {{model.test}}
</body>

plunker: http://plnkr.co/edit/XxeMlVv6I6qOwjPoCUtQ?p=preview

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

2 Comments

Ok. Thanks. And what about label? plnkr.co/edit/ncr6QtZ4Diy2uTHYA7dF?p=preview
Just added scope: true and it works. Thanks for help.

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.