将表单填充到要与ngMessages一起使用的templateURL中
Padding form into templateURL to be used with ngMessages
我对angular很陌生,正在尝试指令。
我希望能够使用
<div ng-messages="form.$error">
<div ng-message="required">You did not enter a field</div>
</div>
在这样的自定义指令中,
<div class="form-group">
<label class="form-label" for="s-input-{{name}}">
{{text.human}}
<span ng-if="text.subtext" class="form-hint">{{text.subtext}}</span>
</label>
<select ng-required="required" ng-model="model" class="form-control" id="s-input-{{name}}" name="{{name}}" ng-options="t for t in text.select"></select>
<div ng-messages="form.$error">
<div ng-message="required">You did not enter a field</div>
</div>
</div>
我可以通过在指令元素下方但不在其中传递ng消息来使其工作。
尽管我真的不知道如何将表单传递到模板中,以便通过ng消息访问它。
我设置了一个plunker来向你展示我的基本设置。
http://plnkr.co/edit/ELx6Fd2eLN4CJjEnzRGM?p=preview
我知道我需要require: '^form',
的表单,尽管我不知道如何在模板URL中使用/访问。
我已经撞到头有一段时间了,所以我真的很感激你的帮助!谢谢
当您需要带有require: '^form'
的表单指令时,您可以在link
函数中访问该FormController。然后,您可以简单地将该控制器连接到指令的作用域,如下所示:
link: function(scope, element, attributes, formController) {
scope.form = formController;
}
然后在模板中引用它:
<div ng-messages="form.$error">
<div ng-message="required">You did not enter a field</div>
</div>
您可以随时将表单传递给指令。
javascript
app.directive('selectGov', function (){
return {
restrict: 'E',
templateUrl: 'select.html',
scope: {
text: "=?",
model: "=",
required: "=",
selectItems: "=?",
name: "@?",
form: "="
},
};
});
html
<select-gov model="person.phoneNumberType" text="q.phoneType" required="true" name="phoneType" form="myForm"></select-gov>
相关文章:
- 转义符不能与innerHTML一起使用
- 高亮显示与数组字符串一起使用时文本插件中断
- setTimeout可以与闭包内的函数一起使用吗
- 将依赖外部库的UMD模块与browserfy捆绑在一起
- Highcharts colorsByPoint与系列中的线性渐变一起使用时不起作用
- 如何将js库与typescript一起使用
- 将webpack和babel与父项目目录中的文件一起使用
- 将 Drag&Drop 与 jsTree 和 DataTables 一起使用
- Javascript-如何让脚本与Ajax请求的数据一起运行
- 将两个ext.TabPanels滚动到一起
- 将jQuery UI Timepicker Addon与React一起使用
- 什么是curl以及如何将其与nodejs一起使用
- Javascript”;onmousemove”;事件无法与类一起使用
- 将require('..')与变量一起使用与在webpack中使用字符串相比
- 如何在php中创建一个函数,该函数与文本区域一起工作,通过输入类似[color:red]的内容来打印具有等效颜色的文本
- 三角库可以与firefox一起使用,但不能在Chrome中使用
- 为什么不't我的ruby代码与javascript文件一起插入
- 通过jQueryAjax一起发布Array和Form数据时出现问题
- 将表单填充到要与ngMessages一起使用的templateURL中
- angularjs的ui-view不能与templateUrl一起工作