如何在AngularJS指令中传递变量值?

时间:2019-05-03 05:23:55

标签: html angularjs angularjs-directive

我在AngularJS中有一条指令,并且在html页面中使用了该指令。该指令需要一个经过硬编码的参数

<md-button class="md-button md-raised md-theme-white list pull-right">
<i class="fa fa-pencil-square-o ng-scope" aria-hidden="true" select-organization-list="Advertiser" click-from="ClickFromSearch" onselect-Custom-Event="OpenModalOrganizationList(org, 'documentCompany')" ng-model='org'></i>
<md-tooltip md-direction="buttom">
Select Advertiser
</md-tooltip>
</md-button>

如何在该指令select-organization-list中传递变量。

我尝试通过在angularJS vm.CompanyType = "Advertiser";和html之类的select-organization-list="vm.CompanyType"中定义变量a来使指令中的值不如“ vm.CompanyType”而不是“ Advertiser”起作用吗?我该如何解决??

这是我的指令

.directive("selectOrganizationList",
            function ($rootScope, $mdDialog, organizationService, $parse, $localStorage, sharedServices, organizationFactory) {
                return {
                    restrict: "A",
                    scope: {
                        ngModel: "=?",
                        //isolatedExpression: '&'
                        onselectCustomEvent: "&"

                    },
                    template: "<div></div>",
                    // controller: "crudgridController as vm",
                    link: function (scope, element, $attrs) {

                        //var CompanyType = $attrs.selectOrganizationList || null;

                        $(element).on("click",
                            function (e) {
                                scope.OpenModalAddContract_OrganizationList = function (ev, valueFor) {
                                    var mynewscope = $rootScope.$new();
                                    scope.itemsOrganization = [];
                                    var te = this;
                                    te.Search = [];
                                    te.Search.PageSize = 10;
                                    te.Search.SearchClick = $attrs.clickFrom;
                                    te.Search.MediaSearch = $attrs.clickFrom;
                                    te.Search.Retired = false;

                                    //////Swoyuj: When clicked from add/edit Contract Page, "PopupOrganization DivisionID" should be "Contract DivisionID"
                                    //if ($attrs.clickFrom == "Contract") {
                                    //    te.Search.DivisionID = parseInt($attrs.divisionId);
                                    //    te.Search.DivisionIDList = [$attrs.divisionId];
                                    //}
                                    if ($attrs.selectOrganizationList == "AdAgency") {
                                        te.Search.CompanyType = "AA";
                                    }

                                    if ($attrs.selectOrganizationList == "Advertiser") {
                                        te.Search.CompanyTypeList = ['AA', 'AD']
                                    }

                                    if ($attrs.selectOrganizationList == "Billboard") {
                                        te.Search.CompanyType = "BO";
                                    }

                                    if ($attrs.selectOrganizationList == "All") {
                                        te.Search.CompanyType = "";
                                    }

2 个答案:

答案 0 :(得分:2)

尝试一下。

restrict: 'EA',
scope: {
   CompanyType: '='
},

然后,您应该可以通过指令中的“ scope.CompanyType”访问变量。

<i class="fa fa-pencil-square-o ng-scope" aria-hidden="true" CompanyType="Advertiser" click-from="ClickFromSearch" onselect-Custom-Event="OpenModalOrganizationList(org, 'documentCompany')" ng-model='org'></i>

答案 1 :(得分:1)

您还可以直接从HTML传递值。

<i class="fa fa-pencil-square-o ng-scope" aria-hidden="true" CompanyType="{{vm.companyType}}" click-from="ClickFromSearch" onselect-Custom-Event="OpenModalOrganizationList(org, 'documentCompany')" ng-model='org'></i>