绑定表单输入自定义指令

时间:2015-06-24 13:00:20

标签: angularjs angularjs-directive angularjs-scope

案例

我有这个自定义指令,它根据发送给它的类型绘制输入字段。

我想添加错误消息以及这些输入字段,以便我们显示合适的错误消息。但由于该指令没有从内部看到该表单,我通过将其作为参数传递来绑定表单本身。

我如何从那里移动所以我可以添加这样的错误信息?

<span class="error" ng-show="myForm.input.$error.required">

现在我的所有尝试都失败了

JS

 app.directive('customDirective', function($compile) {
    return {
        restrict: 'E',
        scope   : {
        type : '=' ,
        name : "="
        form:'='
        } ,
        link : function(scope, element, attrs) {
                scope.$watch(function () {
                        return scope.type ;
                }, function() {

                    var html = '<input type="'+scope.type+'" name="{{name}}" required>';

                    element.html(html);
                    $compile(element.contents())(scope);
                }
                );
            } 
        } 
});

0 个答案:

没有答案
相关问题