如何检查整数/字符串/布尔值的变量内容?

时间:2017-06-12 12:50:27

标签: angularjs angular

如何检查内容类型的varibble?例如,我有规则字符串。这意味着输入变量sholud为"string data"而不是0"0"

我尝试这样做:

var status = false;

                    switch (node.value_type){

                        case "integer":
                            status = angular.isNumber(node.value);
                            break;
                    }

它始终返回false

我试过这个指令:

.directive('checkValueType', function () {
                return {
                    restrict: 'A',
                    link: function (scope, element, attrs) {

                        var status = false;

                        var type_value = attrs.typeValue;

                        scope.$watch(attrs.ngModel, function (v) {

                            switch (type_value){

                                case "integer":
                                    status = angular.isNumber(v);
                                    break;

                                case "string":
                                    status = (typeof v === 'string');
                                    break;

                                case "object":
                                    status = (typeof v === 'object');
                                    break;

                                case "array":
                                case "array_objects":
                                    status = (v instanceof Array);
                                    break;

                                case "boolean":
                                    status = (typeof v === 'boolean');
                                    break;

                                case "float":
                                    status = (Number(v) === n && n % 1 !== 0);
                                    break;

                                case "vector2":
                                    status = (v.length === 2);
                                    break;

                                case "vector3":
                                    status = (v.length === 3);
                                    break;
                            }

                            console.log("Type of " + typeof v);
                            console.log("Type: " + type_value);
                            console.log(status);

                        });
                    }
                };
            })

2 个答案:

答案 0 :(得分:0)

使用 #typeof

switch(typeof(node.value_type)){

  ...

答案 1 :(得分:0)

如果您使用的是typeScript,则可以使用times1 <- strptime(table1[,2], "%d-%b-%Y %I:%M:%S %p") times2 <- strptime(table2[,2], "%d-%b-%Y %I:%M:%S %p") table1a <- data.frame(table1[,1], times1) names(table1a) <- c("id", "date_time") table2a <- data.frame(table2[,1], times2, table2[,3]) names(table2a) <- c("id", "date_time", "cum_freq") table2list <- split(table2a, table2a[,1]) getcumul <- function(x) { x <- table1a[x,] df1 <- table2list[[x$id]] cutt <- findInterval(x$date_time, df1[,2]) if (cutt == 0) return(0) else { cumul <- df1[cutt,3] return(cumul) } } cumuls <- function() sapply(1:nrow(table1a), getcumul)

typeOf