AngularJS - filter for undefined properties in ng-repeat?

前端 未结 4 943
天涯浪人
天涯浪人 2021-02-05 00:43

For my AngularJS project (v1.2.3), I have a list of routes and am trying to build a navigation bar from the object. What I want to do is display any object with an undefined

4条回答
  •  夕颜
    夕颜 (楼主)
    2021-02-05 01:16

    module.filter('notNullOrUndefined', [function () {
        return function (items, property) {
            var arrayToReturn = [];
            for (var i = 0; i < items.length; i++) {
                var test = property !== undefined ? items[i][property] : items[i];
                if (test !== undefined && test !== null) {
                    arrayToReturn.push(items[i]);
                }
            }
            return arrayToReturn;
        };
    }]);
    

    Usage:

    {{style.name}} Background

提交回复
热议问题