角度指令范围。如何隔离单个变量

时间:2016-10-16 21:59:29

标签: javascript angularjs angularjs-directive angularjs-scope

我正在尝试使用angular指令创建输入包装器,它应该有不同的标签。现在我甚至无法访问输入之外的模型(即使范围: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-->

角:

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;
        }
    };
});

现在唯一的想法是找到一种方法来隔离单个变量或类似的东西

1 个答案:

答案 0 :(得分:2)

如前所述,只需使用 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

相关问题