如何在Angular表达式中获取一种范围变量? [英] How to get a type of scope variable in Angular expression?

查看:69
本文介绍了如何在Angular表达式中获取一种范围变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

让我们在范围内定义简单的布尔值:

Let's define simple boolean on the scope:

var mymodal = angular.module('mymodal', []);

mymodal.controller('MainCtrl', function ($scope) {
    $scope.b = false;
});

如何在表达式中获取变量的类型? typeOf Object.prototype.Tostring.call 不起作用。

How can I get type of a variable in the expression? typeOf and Object.prototype.Tostring.call don't work.

<div ng-controller="MainCtrl" class="container">
        <div>
          {{ b }}
          {{ typeOf(b) }}
          {{ Object.prototype.toString.call(b) }}
        </div>
</div>

这是JSFiddle: http://jsfiddle.net/g8Ld80x3/2/

Here's JSFiddle: http://jsfiddle.net/g8Ld80x3/2/

推荐答案

我认为最好的方法是创建一个自定义过滤器并按照您的意愿使用它,你可以检查这个链接,用于获取对象的Object.keys

i think the best way is to create a custom filter and use it as you wish within you expression, you can check this link that use for get the Object.keys of an object

对于你的情况,你可以使用

for your case you can use

angular.module('customfilter', []).filter('typeof', function() {
  return function(obj) {
    return typeof obj
  };
});

这篇关于如何在Angular表达式中获取一种范围变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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