- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个自定义指令,需要另一个自定义指令作为父级:
.directive('controlErrorContainer', function () {
return {
restrict: 'E',
templateUrl: 'template/common/control_error_container.html',
require: '^controlContainer',
...
});
control_error_container.html 如下所示:
<div class="label label-danger" ng-show="isValid" ng-transclude>
</div>
最终,使用这些标签的 html 页面将如下所示:
<control-container control-to-check="stuff">
<input id="stuff" name="stuff" ng-model="stuff" />
<control-error-container>
You have failed me for the last time admiral!
</control-error-container>
</control-container>
我的想法是,control-container 存储对其应该保存和检查的控件的引用,而 control-error-container 仅在观察到的 Controller 存在任何验证问题时才显示其中的错误。
我有一个问题,我希望 controlErrorContainer 在没有任何外部属性的情况下运行。如您所见,它有一个 ng-show 标记,并定义了“isValid”值。该值是不必要的,因为可以通过父自定义指令的容器获取控件有效性的值(我这样做)。这不起作用,我不知道如何使其工作,因为在创建 Controller 后,必须在某处定义它。如果我用 2 路绑定(bind)创建一个作用域,Angular 会抛出异常。
所以问题是如何在不强制用户创建不必要的属性的情况下完成这项工作?
最佳答案
据我了解,您正在尝试构建类似验证框架之类的东西。由于 Angular 已经提供了一个,所以我建议尝试使用它。
Angular 的 validation framework建立在 FormController
之上和NgModelController
form
内部缺少协作 Controller 和ngModel
指令。
添加自定义验证器只是编写一条指令将验证函数挂接到框架中。这种验证器的一个例子是:
app.directive('validUserType', function() {
return {
restrict: 'A',
require: '?ngModel',
link: function(scope, elem, attrs, ngModelCtrl) {
var allowAdmin = attrs.allowAdmin && attrs.allowAdmin === 'true';
if (ngModelCtrl) {
ngModelCtrl.$parsers.push(userTypeValidator);
ngModelCtrl.$formatters.push(userTypeValidator);
}
function userTypeValidator(value) {
if (!value || value === 'guest' || (allowAdmin && value === 'admin')) {
ngModelCtrl.$setValidity('validUserType', true);
return value;
} else {
ngModelCtrl.$setValidity('validUserType', false);
return;
}
}
}
};
});
您申请的是:
<input name="input" ng-model="userType" required valid-user-type allow-admin="true">
请注意,您可以拥有任意数量的验证器( required
也是 Angular 提供的验证器),它们与具有附加属性的普通指令( allow-admin="true"
)没有什么不同。它之所以成为验证器,是因为它与 ngModel
集成的方式。 。您可以在用户提供输入时添加一个“解析器”,并在 JavaScript 代码更改值时添加一个格式化程序。除了验证之外,它还可以充当转换器,即转换字符串 -> 对象(在解析器中),反之亦然(在格式化程序中)。在这种情况下,您需要 2 个独立的函数,而不是通用的函数。
Angular 的框架负责处理剩下的所有事情。它在输入/特定验证器/表单级别维护有效性“标志”,可用于显示/隐藏验证消息或启用/禁用按钮。它还维护 CSS 类,可用于添加视觉糖和“脏”标志,以通知用户哪些字段已被编辑。更重要的是,它将只允许有效值(或 undefined
)传播到您的模型,这意味着如果您的 Controller 中有类似的内容:
$scope.$watch('userType', function(userType) {
if (userType) {
// make ajax with userType
}
});
保证仅有效userType
值将被发送到服务器。
底线:它已经提供了很多功能并且得到了官方维护,因此在它的基础上构建是有意义的。
看看这个 plunker (从 Angular 文档中制作了 plunker)。
当然,您可以使用 control-container
构建自己的验证框架和control-error-container
。您的示例缺少的是实际验证器如何连接到游戏中。我的偏好是将其应用在输入元素上,就像应用 Angular 验证器一样:
<control-container>
<input ng-model="userType" valid-user-type allow-admin="true"/>
<control-error-container>
You have failed me for the last time admiral!
</control-error-container>
</control-container>
valid-user-type
是您的自定义验证器,它将其结果传达给 control-container
然后从 control-container
降至 control-error-container
。这 3 个指令类似于:
app.directive('controlContainer', function() {
return {
restrict: 'E',
replace: true,
transclude: true,
controller: function() {
var controlErrorContainer = null;
this.setValid = function(value) {
if (controlErrorContainer) controlErrorContainer.setValid(value);
}
this.setControlErrorContainer = function(cec) {
controlErrorContainer = cec;
}
},
templateUrl: 'controlContainer.html'
};
});
app.directive('controlErrorContainer', function() {
return {
restrict: 'E',
replace: true,
transclude: true,
require: ['controlErrorContainer', '^controlContainer'],
templateUrl: 'controlErrorContainer.html',
controller: ['$scope', function($scope) {
$scope.isValid = true;
this.setValid = function(value) {
$scope.isValid = value;
};
}],
link: function(scope, elem, attrs, controllers) {
var controlErrorContainer = controllers[0];
var controlContainer = controllers[1];
controlContainer.setControlErrorContainer(controlErrorContainer);
}
};
});
app.directive('validUserType', function() {
return {
restrict: 'A',
require: ['ngModel', '^controlContainer'],
link: function(scope, elem, attrs, controllers) {
var allowAdmin = attrs.allowAdmin && attrs.allowAdmin === 'true';
var ngModel = controllers[0];
var controlContainer = controllers[1];
scope.$watch(
function() {
return ngModel.$viewValue;
},
function(value) {
if (!value || value === 'guest' || (allowAdmin && value === 'admin')) {
controlContainer.setValid(true);
} else {
controlContainer.setValid(false);
}
}
);
}
};
});
你可以做类似<control-container control-to-check="stuff">
的事情但我认为找到“东西”及其值(value)并在其上运行验证器更加困难(我不明白你是如何定义它的)。
查看完整的plunker .
关于javascript - 如何仅从内部修改指令范围属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23802599/
我正在尝试在现有指令的基础上构建一个新指令,但我在我的过程中停止了。加载页面时,我面临以下错误: Multiple directives [directive#1, directive#2] aski
我是 angularjs 世界的新手,我只需要在数字类型的输入中输入从 1 到 10 的数字。不使用 HTML5 的 min 和 max 属性 我在 Jquery 中找到了一个示例,能否帮我将其转换为
我想使用 ionic与 Material 设计。我被困在使用带有自定义 CSS 的 ionic 指令和 angular-material 之间。 我读过使用 ionic 指令我们得到了很多高效的特性,
我创建了以下代码: var node = document.getElementById('TreeList'); var keys = Object.keys(model[0]); var trac
在 AngularJs 中没有提供 ng-enabled 指令。是否有任何适当的理由不在框架中提供该指令,因为当您可以使用 ng- 时,我们同时拥有 ng-show 和 ng-hide隐藏来实现我们的
我最近制作的程序有问题。基本上,它是 John Conway 人生游戏的简单版本,但它运行不正常。问题出在读取单元格及其邻居的状态并决定该单元格的 future 状态的代码中。这是代码的一部分(有点长
Dockerfile reference关于 FROM 指令的内容如下: FROM can appear multiple times within a single Dockerfile in or
我一直在尝试理解指令中孤立作用域和继承作用域之间的区别。这是我准备让自己理解的一个例子: HTML Inside isolated scope directive: {{m
知道如何从指令内部访问属性值吗? angular.module('portal.directives', []) .directive('languageFlag', ['$r
我正在通过将 c 程序与其等价的汇编程序进行比较来学习汇编。 这是代码。 .file "ex3.c" .section .rodata .LC0: .string "I am %d
我正在尝试写一个 Jenkinsfile并行执行一系列步骤。目标是拥有两个 agents (又名。 nodes )。一个应该进行 Windows 构建,另一个应该进行 linux 构建。但是,我不希望
我想知道为什么指令 FYL2XP1在 x86 架构上精确计算数学公式 y · log2(x + 1)。 这个公式有什么特别之处? 最佳答案 y操作数通常是编译时常量,暂时忘记 x + 1 . 自 lo
这个问题已经有答案了: Parameterize an SQL IN clause (41 个回答) 已关闭 8 年前。 第一个声明: Select GroupMember FROM Group 结果
我从 this question fork 并编辑了一个 plunker 我想做的是在数据加载后更新/填充 SELECT 元素(组合框),但有些事情不对劲。我检索数据,它位于 SELECT 元素的范围
我想创建一个简单的 markdown 指令,它接受元素中的一些内容,解析它并用 html 替换它。 所以这样: #Heading 或这个(其中 $scope.heading = '#Heading';
我对 Ansible 还很陌生,对于我对 local_action 指令的理解有一个简单的问题。 这是否意味着该命令完全在本地执行?假设你有这样的东西: local_action: command w
我有以下 HTML: ... ... 以及以下指令: myApp.directive('specialInput', ['$timeout', function($timeout)
如何在 .htaccess 中创建 Apache 指令强制文件 .mp4和 .pdf去下载?目前它们出现在浏览器窗口中。相反,我希望出现一个下载文件对话框。 最佳答案 将以下内容添加到 .htacce
我的问题是关于 C 中的 fork() 指令。我有以下程序: void main(){ int result, status; result = fork(); if(result=
我想要一个类似于 ng-model 的属性指令。我只想另外将一个输入字段值绑定(bind)到一个范围变量(只是在一个方向输入字段 ->范围变量)。所以我刚刚尝试了这个指令,但无论如何我都无法调用该指令
我是一名优秀的程序员,十分优秀!