也许这是一个新手的错误,但我似乎无法访问它$scope.model,$ngModelController所以我可以抓住$viewValue它.
我有一个没有表单的输入(我使用ui-mask指令):
<input type="text" ng-model="inicio" name="inicio" ui-mask="99/99/9999">
Run Code Online (Sandbox Code Playgroud)
// inside my controller
$scope.inicio = dateFilter((new Date).getTime(), 'dd/MM/yyyy');
Run Code Online (Sandbox Code Playgroud)
ui-mask将$ modelValue设置为与$ viewValue不同的值,这使得很难将格式化数据发送到服务器.当$scope.inicio模型更改时,该值是没有斜杠的日期,例如01012014.所以我需要能够为该输入获取控制器,但不必将其包装在表单中,并且必须使用$scope.myForm.inicio.$viewValue.它必须是可能的......
我知道我可以做的事情,但似乎很骇客,必须有一个更简单的方法:
$scope.myForm.input.$viewValue$('input[name="inicio"]').data('$ngModelController');angular.element('input[name="inicio"]').controller('ngModel');app.directive('viewValue', function(){
return {
priority: 10,
require: 'ngModel',
link: function(scope, element, attrs, controller){
scope.$watch(attrs.viewValue, function(newValue, oldValue){
if (newValue !== oldValue){
scope[attrs.viewValue] = controller.$viewValue;
}
});
}
}
});
Run Code Online (Sandbox Code Playgroud)
<input type="text" ui-mask="99/99/9999" ng-model="inicio" view-value="inicio">
Run Code Online (Sandbox Code Playgroud) 我目前正在使用它directiveElement.data("$ngModelController")来访问元素$ngModelController,如下例所示.
describe("directiveElement", function () {
it("should do something with ngModelController", inject(function($compile, $rootScope) {
var directiveElement = $compile("<input ng-model="myNgModel" customDirective type="text"></input>")($rootScope);
$rootScope.$digest();
var ngModelCtrl = directiveElement.data("$ngModelController");
ngModelCtrl.$modelValue = "12345";
// do rest of test
}));
});
Run Code Online (Sandbox Code Playgroud)
但是,我想知道是否有更好的访问权限$ngModelController,或者访问是否$ngModelController是一个坏主意?