在AngularJS中,我想測試一個指令內的布爾值,但是該值是以字符串形式返回的。在AngularJS中,爲什麼布爾參數被評估爲字符串?
下面是代碼:
angular.module('TestApp', ['TestApp.services', 'TestApp.controllers', 'TestApp.directives']);
angular.module('TestApp.services', ['ngResource']).
factory('Obj', function($resource){
return $resource('datas.json');
});
angular.module('TestApp.controllers', []).
controller('TestCtrl', ['$scope', 'Obj', function($scope, Obj) {
$scope.objs = Obj.query();
}]);
angular.module('TestApp.directives', []).
directive('requiredStatus', function() {
return function(scope, elm, attrs) {
attrs.$observe('v', function(av) {
if (attrs.completed) {
scope.val= true;
} else {
scope.val= false;
}
scope.type = typeof attrs.completed;
});
};
});
http://plnkr.co/edit/DvIvySFRCYaz4SddEvJk
我應該怎麼做纔能有指令內的typeof 「布爾」?
插的屬性值總是字符串看到這個答案http://stackoverflow.com/a/12372494/527968 –