2016-03-08 75 views
7

讓我們定義的範圍簡單的布爾:如何在Angular表達式中獲得一個範圍變量的類型?

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

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

我怎樣才能在表達式中的變量的類型? typeOfObject.prototype.Tostring.call不起作用。

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

這裏的jsfiddle:http://jsfiddle.net/g8Ld80x3/2/

回答

10

我認爲最好的方法是創建一個自定義過濾器,當你希望你的表達中,可以檢查this鏈接,用於獲取對象使用它。爲你的情況下,對象

的鍵,你可以使用

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

好主意!簡單而強大。我喜歡。 – Landeeyo

+0

爲什麼沒有內置到Angular中?也許它已經在Angular 2中。雖然看起來不像它http://stackoverflow.com/questions/37511055/how-to-check-type-of-variable-in-ngif-in-angular2。 –

1

嘗試在控制器這樣的事情(如你想要做的,我不認爲是可能直接在表達式):

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

 
mymodal.controller('MainCtrl', function ($scope) { 
 
    $scope.b = false; 
 
    $scope.getBType = function(test){ 
 
     return(typeof test); 
 
    } 
 
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script> 
 
<div ng-app='mymodal' ng-controller="MainCtrl" class="container"> 
 
     <div> 
 
      {{ getBType(b) }} 
 
     </div> 
 
</div>

+0

我不想在控制器中執行此操作。我想在表達式中執行typeof。同樣在'ng-if'或'ng-show' /'ng-hide'指令中。 – Landeeyo

+0

首先,您必須在控制器中執行該操作,然後將其返回給表達式。 – Ionut

+0

不應該這樣比返回字符串而不是調用alert? – Landeeyo

4

你不能這樣做,並有充分的理由:Angular expression解析器不允許這樣的在模板中。

如果你真的希望能夠這樣做,我建議明確設定在$rootScope輔助方法,所以它會在您所有的模板可用:

mymodal.run(function($rootScope) { 
    $rootScope.typeOf = function(value) { 
     return typeof value; 
    }; 
}); 

,你甚至可以參考角度自己的實用方法:

mymodal.run(function($rootScope) { 
    ['isArray', 'isDate', 'isDefined', 'isFunction', 'isNumber', 'isObject', 'isString', 'isUndefined'].forEach(function(name) { 
     $rootScope[name] = angular[name]; 
    }); 
}); 

並在模板中使用{{ isArray(arr) }}

+0

這是非常好的答案,非常感謝。但是Zamboney創建過濾器的想法對我來說更好。無論如何 - 再次感謝。 – Landeeyo

+0

其實,在這種情況下,過濾器是一個非常糟糕的主意,至少有兩個原因,但是對你來說更好。 – dfsq

+0

有什麼缺點? – Landeeyo

0

當您嘗試訪問特定值的typeof類型和當前代碼時,您在view中執行此操作,這對於此類操作來說相當晚。

相反,你可以做一個函數在控制器的範圍,只是從那裏返回它:

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

 
mymodal.controller('MainCtrl', function ($scope) { 
 
    $scope.b = false; 
 
    $scope.getTypeof = function(it){ return typeof(it); }; 
 
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script> 
 
<div ng-app='mymodal' ng-controller="MainCtrl" class="container"> 
 
     <div> 
 
      {{ b }} : {{ getTypeof(b) }} 
 
      
 
     </div> 
 
</div>

0

HTML

<div ng-controller="MainCtrl" class="container"> 
     <div> 
      {{ b }}<br> 
      {{ typeOf(b) }}<br> 
     </div> 
</div> 

JS

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

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

結果

false 
boolean 
2

只是爲了顯示應用到我的示例代碼Zamboney的回答是:

控制器:

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

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

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

查看:

<div ng-controller="MainCtrl" class="container"> 
    <div> 
    {{ b }} 
    {{ b | getType }} 
    <div ng-if="(b | getType) == 'number'"> 
     It's a number 
    </div> 
    <div ng-if="(b | getType) == 'boolean'"> 
     It's a boolean 
    </div> 
    </div> 
</div> 

和小提琴:http://jsfiddle.net/g8Ld80x3/5/

相關問題