Angular:如何在启动指令时使用三元组?

时间:2017-01-13 16:55:28

标签: angularjs angularjs-directive

我试图以这种方式从我的jsp发起指令:

<query-builder group="addEditRuleSetCtrl.filter.group" fields="{{ addEditRuleSetCtrl.origin }}=='DDE'&&addEditRuleSetCtrl.fieldsDDE||addEditRuleSetCtrl.fieldsATT" ></query-builder>

其中addEditRuleSetCtrl与我的控制器不同。我在这里尝试的逻辑是:如果origin ==“DDE”,则使用从数组fieldsDDE中选择的字段值启动指令,否则使用fieldsATT。

我收到错误:

Error: [$parse:syntax] Syntax Error: Token '{' invalid key at column 2 of the expression [{{ addEditRuleSetCtrl.origin }}=='DDE'&&addEditRuleSetCtrl.fieldsDDE||addEditRuleSetCtrl.fieldsATT] starting at [{ addEditRuleSetCtrl.origin }}=='DDE'&&addEditRuleSetCtrl.fieldsDDE||addEditRuleSetCtrl.fieldsATT]. 

我希望使用三元选项来启动指令。有什么建议吗?

这是我的指示:

AddEditRuleSetApp.directive('queryBuilder', ['$compile', function ($compile)        {
return {
    restrict: 'E',
    scope: {
        group: '=',
        fields: '='
    },

    /*controller: 'AddEditRuleSetModalController',
    link:function (scope,element, attrs) {

    },*/
    templateUrl: '/queryBuilderDirective.html',
    compile: function (element, attrs) {
        var content, directive;
        content = element.contents().remove();
        return function (scope, element, attrs) {

            //scope.group = '{"group": {"operator": "AND","rules": []}}';
            window.alert("Directive: " + JSON.stringify( scope.group));

            scope.ruleConditionSetOperators = [
                { name: 'AND' },
                { name: 'OR' }
            ];

            /*scope.fields = [
                { name: 'Firstname' },
                { name: 'Lastname' },
                { name: 'Birthdate' },
                { name: 'City' },
                { name: 'Country' }
            ];*/

            scope.ruleConditionOperators = [
                { name: '=' },
                { name: '<>' },
                { name: '<' },
                { name: '<=' },
                { name: '>' },
                { name: '>=' }
            ];

            scope.addCondition = function () {
                scope.group.rules.push({
                    condition: '=',
                    field: 'Firstname',
                    data: ''
                });
            };

            scope.removeCondition = function (index) {
                scope.group.rules.splice(index, 1);
            };

            scope.addGroup = function () {
                scope.group.rules.push({
                    group: {
                        operator: 'AND',
                        rules: []
                    }
                });
            };

            scope.removeGroup = function () {
                "group" in scope.$parent && scope.$parent.group.rules.splice(scope.$parent.$index, 1);
            };

            directive || (directive = $compile(content));

            element.append(directive(scope, function ($compile) {
                return $compile;
            }));
        }
    }
}
}]);

这是我的控制器:

AddEditRuleSetApp.controller( 'AddEditRuleSetModalController', [ '$scope', '$rootScope', '$modalInstance','RuleEngineService', 'origin', 'iceField',
function( $scope, $rootScope , $modalInstance, RuleEngineService, origin, iceField )
{
   self.origin = origin;

   self.fieldsDDE = [
            { name: 'FirstnameDDE' },
            { name: 'LastnameDDE' },
            { name: 'BirthdateDDE' },
            { name: 'CityDDE' },
            { name: 'CountryDDE' }
        ];

        self.fieldsATT = [
            { name: 'FirstnameATT' },
            { name: 'LastnameATT' },
            { name: 'BirthdateATT' },
            { name: 'CityATT' },
            { name: 'CountryATT' }
        ];
 }

1 个答案:

答案 0 :(得分:0)

为什么不使用某个功能?

<query-builder group="addEditRuleSetCtrl.filter.group" fields="addEditRuleSetCtrl.getCorrespondingFields()" ></query-builder>

self.getCorrespondingFields = function(){
    return self.origin == 'DDE' ? self.fieldsDDE : self.fieldsATT;
};