如何将模型的值存储在变量中?
问题描述:
我正在创建一个指令,我想在其中比较存储在模型中的值(比如名称"作为模型,它的值为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);
}
}
})