【发布时间】:2017-08-09 10:56:13
【问题描述】:
我有一个模式被加载到一个指令中,它有一个带有一些属性的按钮。
app.directive('dataFactoryModal', ['$compile', '$timeout', function($compile, $timeout) {
return {
scope: { .... }
link: function (scope, element, attrs) {
var html = '
<input ng-model = "recipients" name = "email" type = "text" placeholder = "Enter email(s)" >\
....
// Modal button section
<button type = "button" class = "btn btn-primary" data-factory = "{{dataFactoryCodes}}" data-recipients = "">Submit</button>\
....
';
....
}
}
}
现在我需要从 "data-list 中的 输入(使用 ng-model 'Recipients')插入所有键入的电子邮件 " 按钮的属性。
如下所示:
<button type = "button" class = "btn btn-primary" data-factory = "123;109;129" data-recipients = "meme@email.com;yayaya@email.com">Submit</button>
在输入收件人中,您可以键入多个电子邮件地址,但只能用逗号分隔。
当然,ng-model 可以提供帮助。所以...
<button type = "button" class = "btn btn-primary" data-factory = "{{dataFactoryCodes}}" data-recipients = "{{recipients}}">Submit</button>
但棘手的部分是用分号替换所有逗号。
到目前为止,我在指令中添加了 ff。但没有成功。
-
直接替换过滤器
scope.replaceCommas = function(value) { if (value!==undefined) { return value.replace(',', ';'); } }然后调用属性内的函数,如:
data-list = {{replaceCommas(recipients)}}结果:
data-list = "email@email.com;email2@email.com,email3@email.com"它只是替换第一个逗号,而不是随后添加的电子邮件。
-
我也尝试使用
$watch,但没有成功。scope.$watch('recipients', function(newValue, oldValue) { scope.email = newValue; // if I did this, this would replace all commas with semicolons on the button attribute AND on the textfield scope.email = newValue.replace(',', ';'); // if I did this, this would just replace only the first comma scope.emailTags = newValue.replace(',', ';'); }
然后,在按钮上...
data-list = {{emailTags}}
为什么这不起作用?
【问题讨论】:
-
不是关于您的问题,只是评论:在节点属性中的等号字符之间放置空格是不好的:
type = "button"不行,更喜欢type="button"。它可能会在 html 有效性检查器中触发警告。
标签: javascript angularjs