有条件地为属性分配值

时间:2014-08-17 14:58:31

标签: javascript object

$scope.survey.pages[0].fields.push({
                    type:type,
                    required:false,
                    **checkbox:true,**
                    **ui:(this.checkbox)** ? 'checkbox' : 'radio',
                    quest:[{answer:''},{answer:''},{answer:''}],
                    id:'name'+(++$scope.name),
                    allowId:'allow'+$scope.name,
                    requireId:'require'+$scope.name,
                    otherId:'other'+$scope.name
                });

这就是我想要的,如果复选框属性为真,那么我想分配文本'复选框',如果没有,收音机......我错过了什么?

1 个答案:

答案 0 :(得分:0)

为什么不让ui成为一个函数,即:

            $scope.survey.pages[0].fields.push({
                type:type,
                required:false,
                **checkbox:true,**
                **ui:function(){return (this.checkbox)** ? 'checkbox' : 'radio';},
                quest:[{answer:''},{answer:''},{answer:''}],
                id:'name'+(++$scope.name),
                allowId:'allow'+$scope.name,
                requireId:'require'+$scope.name,
                otherId:'other'+$scope.name
            });

test=[];
test.push({checkBox:true, ui: function(){return (this.checkBox? "TRUE":"FALSE")}});
test.push({checkBox:false, ui: function(){return (this.checkBox? "TRUE":"FALSE")}});
console.log(test[0].ui(), "    ", test[1].ui())