2013-04-18 14 views

回答

2

使用自然模型设置器scope[attrs.ngModel]代替setviewvalue。

app.directive('format', function($filter) { 
    return { 
    require: 'ngModel', 
    link: function(scope, element, attrs, ctrl) { 
    element.unbind('input').unbind('keydown').unbind('change'); 
     element.bind('blur', function() { 
      if (element.val()) { 
       scope.$apply(function() { 
        scope[attrs.ngModel] = element.val(); 
       });   
      } 
     }); 

     ctrl.$formatters.unshift(function(modelValue) { 
      if (modelValue) { 
       var formatted = $filter('currency')(modelValue); 
       return formatted; 
      } 
     }); 
    } 
} 
}); 

您还需要解析器才能正常工作。

+2

darn,如果ng-model类似于ng-model =“entry.test”,那么该怎么办?它在ng-repeat中,你知道.. – foxx

+0

只是做一个attrs.ngModel.split(“。”)然后迭代通过作用域属性 – roemer

+1

这很漂亮 – cdmckay