2015-10-16 183 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}