如何将变量传递给指令?

时间:2016-08-07 07:44:40

标签: angularjs

我有这个指令:

app.directive('dropdown', ['$timeout',function ($timeout) {
    return {
        require: '^ngModel',
        restrict: 'E',
        template: '<div id="dropdownDirective" class="btn-group" dropdown>' +
        '<button class="btn dropdown-toggle" ng-disabled="ngDisabled" dropdown-toggle>' +
        '{{ items[ngModel].name }}' +
        '<span class="caret"></span>' +
        '</button>' +
        '<ul class="dropdown-menu" role ="menu" aria-label="dLabel">' +
        '<li ng-repeat="item in items">' +
        '<a href="#" ng-bind="item.name" ng-click="select(item)" >< / a >' +
        '</li>' +
        '</ul>' +
        '</div>',
        scope: {
            ngModel: '=', // selected item
            items: '=',   // items to select from
            ngDisabled: '=',
            ngChange: '&'
        },
        link: function (scope: any, element, attrs, ngModelCtlr) {
            // selection changed handler
            scope.select = function (item, ngModel) {
                scope.ngModel = item.id;
                $timeout(scope.ngChange, 0);
                ngModelCtlr.$setDirty();
            };
        }
    }
}]);

我称之为:

<dropdown ng-model="phs.phrases[row.index].posId"
          items="phs.phrasePosShortNames">
</dropdown>

我想要做的是能够调用该指令并传入一个名为noButtonBorder的参数。然后,如果传递了该参数,我希望将类“noBorder”添加到按钮中。

1 个答案:

答案 0 :(得分:1)

scope: {
        ngModel: '=', // selected item
        items: '=',   // items to select from
        ngDisabled: '=',
        ngChange: '&',
        noButtonBorder: '='
    },
link: function (scope: any, element, attrs, ngModelCtlr) {
        // selection changed handler
        if(noButtonBorder){
       attrs.class="noBorder";
         }
        };