元素类型指令时未使用$ ViewValue

时间:2015-12-29 13:36:21

标签: angularjs angularjs-directive angularjs-ng-model

我创建了这个使用格式化程序的简单指令,当指令用作元素或者它是div的属性时,它不会更新输入。

JSBIN: http://jsbin.com/rifaxuvihu/edit?html,js,output

    angular.module('myApp', [])

    .controller('myCtrl', function($scope) {

  $scope.foo = '123';
  console.log('------ MODEL CHANGED ($scope.foo = "123") ------');

  $scope.changeModel = function() {
    $scope.foo = 'abc';
    console.log('------ MODEL CHANGED ($scope.foo = "abc") ------');
  };

})

.directive('myDirective', function() {
  return {
                restrict: 'AE',
                transclude: true,
                require: 'ngModel',
                template: function($element, $attrs, $ctrls) {
                    var inp, lbl = 'label'; 

                    if ($attrs.type == 'html'){
                        inp = [
                            '<summernote class="form-control" ng-model="ngModel" ',
                            '   name="{{name}}" placeholder={{placeholder}} height="{{height}}"></summernote>'
                        ];
                        lbl = 'div';
                    } else
                        inp = [
                            '<input class="form-control" ng-model="ngModel" ',
                            '   type="{{type}}" />'
                        ];

                    var ret = [
                        '<div class="advform-input form-group inp-{{type}} {{cols}}">',
                        '   <' + lbl + ' class="advform-lbl">',
                        inp.join('\n'),
                        '       <div class="helper" ng-if="type != \'html\'">{{ placeholder }}</div>',
                        '       <div class="desc">{{ desc }}</div>',
                        '   </' + lbl + '>',
                        '</div>'
                    ].join('\n');

                    return ret;
                },
                scope: {
                    placeholder: '@',
                    ngModel: '=?',
                    type: '@',
                    name: '@',
                    height: '@'
                },
                link: function ($scope, $element, $attrs, $ngModel) {
                    console.log('ooi');
                    if (true){

                        $ngModel.$formatters.unshift(function(modelVal) {
                            console.log('-- Formatter --', JSON.stringify({
                              modelVal:modelVal,
                              ngModel: {
                                viewVal: $ngModel.$viewValue,
                                modelVal: $ngModel.$modelValue
                              }
                            }, null, 2));
                            return 1 + '-) ' +modelVal;
                          });

                          // same as $watch('foo')
                          $scope.$watch(function() {
                            return $ngModel.$viewValue;
                          }, function(newVal) {
                            console.log('-- $watch "foo" --', JSON.stringify({
                              newVal:newVal,
                              ngModel: {
                                viewVal: $ngModel.$viewValue,
                                modelVal: $ngModel.$modelValue
                              }
                            }, null, 2));
                          });
                    }
                }
  };
})

;

html:

<!DOCTYPE html>

<html ng-app="myApp" ng-controller="myCtrl">

<head>

  <script src="//ajax.googleapis.com/ajax/libs/angularjs/1.3.0-beta.18/angular.min.js"></script>

</head>

<body>
<form name="form">
  <my-directive type="text" name="foo" ng-model="foo"></my-directive>
  <input my-directive type="text" name="foo" ng-model="foo" />
  <div my-directive type="text" name="foo" ng-model="foo"></div>
</form>
<button ng-click="changeModel()">Change Model</button>
  <p>$scope.foo = {{foo}}</p>
  <p>Valid: {{!form.foo.$error.test}}</p>
</body>

</html>

1 个答案:

答案 0 :(得分:1)

问题是该指令创建了一个新范围,该指令的ngModel的viewValue与模板中创建的不同。
要修复它,您可以将模板的ng模型更改为$ parent.ngModel 因此格式将按预期工作。