检查指令中是否存在属性

时间:2015-10-16 16:45:29

标签: angularjs

我想检查指令中是否存在某个属性,而我不知道该怎么做,有人可以帮忙吗?这可能很简单,但我是棱角分明的

(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
}


})();

3 个答案:

答案 0 :(得分:1)

&&

答案 1 :(得分:0)

我知道这是一个老问题,但还有另一种检查属性是否存在的方法

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

答案 2 :(得分:0)

指令对象上的

将范围对象更改为  scope: { showText: "=?" }, '?'使属性为可选, 然后在控制器中检查属性是否存在angular.isUndefined(vm.showText)typeof vm.showText === 'undefined'

有关更多信息,请查看angular 1.7.x - Error: $compile:nonassign Non-Assignable Expression