如何存储模型在一个变量的值? [英] How to store the value of model in a variable?

查看:154
本文介绍了如何存储模型在一个变量的值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建一个指令,在其中我想比较存储在模型中的值(说名为模型,它是有值角)
要被存储在变量中。

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.

我想是这样的下方,

var nameValue=name;

但它(nameValue)不具有价值角度

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-模型)

Can anyone tell me how to retrieve the value stored in model(ng-model)

推荐答案

您可以简化事情有点与 NG-检查和链接功能:

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 = preVIEW

Here is a working demo: http://plnkr.co/edit/owTLnPUbbZIhwUtfoivt?p=preview

这篇关于如何存储模型在一个变量的值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆