【发布时间】:2016-01-22 09:59:47
【问题描述】:
我有一个指令,它基本上是一个自定义输入字段
JS(InputFieldDirective.js):
function InputFieldDirective() {
var directive = {
restrict: 'EA',
replace: true,
templateUrl: '/Views/Directives/InputField.html',
scope: {
type: '@inputType',
pattern: '@inputPattern',
id: '@inputId',
class: '@inputClass',
mandatory: '@inputMandatory',
autocomplete: '@inputAutocomplete',
bindedModel: '=inputModel',
placeholder: '@inputPlaceholder',
icon: '@inputIcon',
errorMessage: '@errorMessage'
}
}
return directive;
};
HTML (InputField.html):
<div class="input-field-wrapper">
<i class="placeholder-icon {{ icon }}" ng-if="icon !== ''"></i>
<input type="{{ type }}" pattern="{{ pattern }}" id="{{ id }}" name="{{ id }}" class="{{ class }}" ng-required="mandatory == 'yes'" autocomplete="{{ autocomplete }}" ng-model="bindedModel" ng-class="{'empty': !bindedModel}">
<label class="placeholder-label">{{ placeholder }}</label>
<span class="focus-animation"></span>
所以当我尝试将它插入到这样的随机模板文件中时:
<input-field input-type="text" input-pattern="" input-id="loginEmail" input-class="form-control" input-required="true" input-autocomplete="off" input-model="login.loginData.username" input-placeholder="Email" input-icon="em-email" error-message="Invalid email address!"></input-field>
然后它就像一个魅力。将数据模型(在模板的控制器中处理和使用)连接到字段。例如:如果我记录模型“loginData”(包含“用户名”和“密码”模型),当我在输入中输入内容时,我会得到正确的结果。例如:
Input values: username: myUsername
password: myPassword
loginData model: {username: 'myUsername', password: 'myPassword'}
但是,如果我将输入类型更改为“电子邮件”或“网址”(使用其他一些值进行测试,“文本”、“密码”、“日期”、“搜索”等效果很好),那么我开始输入输入中的某些内容,loginData 的“用户名”变量神奇地消失了。例如:
Input values: username: myUsername
password: myPassword
loginData model: {password: 'myPassword'}
我做错了什么?将模型连接到指令的元素有什么问题吗?但如果是,为什么它只出现在输入类型“email”和“url”(可能与其他一些我没有测试过的)并且与其他人一起工作?
【问题讨论】:
标签: javascript angularjs angularjs-directive