2015-10-16 179 views
1

我想檢查指令中是否存在屬性,我不知道該怎麼做,任何人都可以幫忙嗎?這可能是簡單的,但我是新來的角檢查指令是否存在屬性

(function() { 
'use strict'; 

angular.module('app').directive('home', home); 

var strVar=""; 
strVar += "<home show-text=vm.text></home>" 

function home() { 
return { 
     restrict: 'E', 
     transclude: true, 
     template: strVar, 
     scope: { 

      showText: "=" 
     }, 
     controller: HomeController, 
     controllerAs: 'vm', 
     bindToController: true 
}; 
}; 

HomeController.$inject = ['$scope','$rootScope', '$timeout','$sce']; 
function HomeController($scope,$rootScope,$timeout,$sce) { 
if(vm.showText == 「undefined」) 
{ 
//Run some code 
} 


})(); 

回答

1
if (angular.isUndefined($scope.showText)) { 
    //do stuff 
}); 
+0

那對我來說不工作 – user5277847

+0

是檢查該指令中是否存在屬性? – user5277847

+0

我改變了showText後,我得到了這個工作:「=」showText:「&」 –

0

我知道這是一個老問題,但還有另一種方式來檢查,如果屬性存在

if(angular.isDefined(vm.content.title)){ //do something}