将属性从custom指令复制到输入

lat*_*ata 5 angularjs

我有一个自定义的driective,用div包装输入并添加一个标签.

<my-input label="My Label" name="myname" ng-model="mymodel" ng-pattern="/^[a-z]+$/">
Run Code Online (Sandbox Code Playgroud)

我想可选地使用所有可能的角度指令来输入像ng-pattern,ng-minlength等.现在它看起来像这样:

app.directive('myInput',[function () {
    return {
        restrict: "E",
        replace: true,
        scope: {
            ngModel: '=',
            name: '@',

            ngMinlength: '=',
            ngMaxlength: '=',
            ngPattern: '@',                
            label: '@'                
        },
        compile: function(element, attrs){

            if(!_.isUndefined(attrs['ngMinlength'])) {
                element.find('input').attr('ng-minlength', 'ngMinlength');
            }
            if(!_.isUndefined(attrs['ngMaxlength'])) {
                element.find('input').attr('ng-maxlength', 'ngMaxlength');
            }                
            if(!_.isUndefined(attrs['ngPattern'])) {
                element.find('input').attr('ng-pattern', attrs['ngPattern']);
            }               


        },
        template: '<div class="form-group">'
        + '<label>{{ label | translate }}</label>'
        + '<div>'
        + '<input type="text" class="form-control input-sm" name="{{ name }}" ng-model="ngModel">'           
        + '</div></div>'
    };
}]);
Run Code Online (Sandbox Code Playgroud)

问题是我想要使用ng-pattern与输入中的ng-pattern完全相同,所以我希望有可能在ng-pattern中使用regexp,并且还可以使用pattern($scope.mypattern = /^[a-z]+$/; ... ng-pattern="mypattern")来使用范围变量.如何管理?

我想要两个都工作:

1.

<my-input label="My Label" name="myname" ng-model="mymodel" ng-pattern="/^[a-z]+$/">
Run Code Online (Sandbox Code Playgroud)

2.

$scope.myPattern = /^[a-z]+$/
...
<my-input label="My Label" name="myname" ng-model="mymodel" ng-pattern="myPattern">
Run Code Online (Sandbox Code Playgroud)

flo*_*bon 1

我给你三个答案。

  1. 一般来说,不建议同时支持模型属性和直接字符串。这种情况由指令范围内的声明处理=,如果您想传递字符串,则可以使用简单的引号。例如 ngBind 的工作原理如下:ng-bind="someModel"或者ng-bind="'some string'"

  2. 如果你真的愿意,你可以尝试解析该表达式。如果它是可解析的,则意味着它是一个作用域模型。否则,它可能是一个字符串。请参阅下面的代码片段中的工作示例:

angular.module('test', [])

.controller('test', function($scope) {
  $scope.model = "string from scope model";
})

.directive('turlututu', function($parse) {
 return {
   restrict: 'E',
   scope: {},
   template: '<div class="tu">{{content}}</div>',
   link: function(scope, elem, attrs) {
     try {
       scope.content = $parse(attrs.text)(scope.$parent);
     } catch(err) {
     } finally {
       if (!scope.content) {
         scope.content = attrs.text;
       }
     }
   }
 };
});
Run Code Online (Sandbox Code Playgroud)
body { font-family: monospace; }

.tu { padding: 10px; margin: 10px; background: #f5f5f5; border-bottom: 2px solid #e5e5e5; }
Run Code Online (Sandbox Code Playgroud)
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="test" ng-controller="test">
  
  <turlututu text="hardcoded string"></turlututu>
  <turlututu text="model"></turlututu>
  
</div>
Run Code Online (Sandbox Code Playgroud)

  1. 就 ngPattern 而言,因为对代码的好奇心总是对你有帮助,你可以在 Angular 的源代码中看到,他们测试属性的第一个字符:如果是,/则被视为正则表达式,否则被视为作用域模型(请参见下面的示例)

angular.module('test', [])

.controller('test', function($scope) {
  $scope.model = /[a-z]*/;
})

.directive('turlututu', function($parse) {
 return {
   restrict: 'E',
   scope: {},
   template: '<div class="tu">{{content}}</div>',
   link: function(scope, elem, attrs) {
     if (attrs.regexp.charAt(0) === '/') {
       scope.reg = new RegExp( attrs.regexp.substring(1, attrs.regexp.length-1) );
     } else {     
       scope.reg = new RegExp( $parse(attrs.regexp)(scope.$parent) );
     }
     
     scope.content = scope.reg.toString()
   }
 };
});
Run Code Online (Sandbox Code Playgroud)
body { font-family: monospace; }

.tu { padding: 10px; margin: 10px; background: #f5f5f5; border-bottom: 2px solid #e5e5e5; }
Run Code Online (Sandbox Code Playgroud)
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="test" ng-controller="test">
  
  <turlututu regexp="/[0-9]*/"></turlututu>
  <turlututu regexp="model"></turlututu>
  
</div>
Run Code Online (Sandbox Code Playgroud)