如何将模型的值存储在变量中? [英] How to store the value of model in a variable?
本文介绍了如何将模型的值存储在变量中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在创建一个指令,我想在其中比较存储在模型中的值(比如名称"作为模型,它的值为Angular")存储在一个变量中.
I am creating a directive, in which i want to compare the value stored in a model (say "name" as model and it is having value "Angular") to be stored in a variable.
我尝试过类似下面的方法,
I tried something like below,
var nameValue=name;
但它(nameValue)没有值Angular"
but it(nameValue) is not having the value "Angular"
Example:
app.directive('kmRadio', function() {
return{
restrict:'E',
compile: function(element,attrs) {
var model=ngModelName;
console.info(attrs.kmModel);
console.info("{{'+attrs.kmModel+'}}");
if(attrs.kmTitle==model) {
previewString="<input type="+"radio"+" checked>";
}
else {
previewString="<input type="+"radio"+">";
}
var htmlText=''+previewString+''+
element.replaceWith(htmlText);
}
}
})
谁能告诉我如何检索存储在模型(ng-model)中的值
Can anyone tell me how to retrieve the value stored in model(ng-model)
推荐答案
你可以使用 ng-checked
和一个链接函数来简化一些事情:
You can simplify things a bit with ng-checked
, and a link function:
app.directive('kmRadio', function() {
return {
restrict: 'E',
scope: true,
replace: true,
template: "<input type='radio' ng-checked='title == model' />",
link: function(scope, element, attrs) {
scope.model = scope.$eval(attrs.kmModel);
scope.title = attrs.kmTitle;
console.info('attrs.kmModel: ', attrs.kmModel);
console.info('scope.$eval(attrs.kmModel):', scope.model);
}
}
})
这是一个工作演示:http://plnkr.co/edit/owTLnPUbbZIhwUtfoivt?p=预览
这篇关于如何将模型的值存储在变量中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文