簡體   English   中英

如何在Angular指令中將參數傳遞給函數?

[英]How to pass parameters to a function within an Angular directive?

AngularJS指令內 ,我觸發了一個回調函數,該回調函數需要訪問已注入的指令級對象。

我為此使用KendoUI模板函數,但是我認為這是關於作用域而不是函數的問題。

指示:

app.directive('projectEditorGrid', function (dataSourceFactory) {

    var dataSourceFactory = new dataSourceFactory("/odata/ProjectEditor");

    return {
        link: function ($scope, $element, $attrs) {
            $element.kendoGrid({
                dataSource: dataSourceFactory.projects(),
                pageable: true,
                height: 400,
                toolbar: ["create"],
                columns: [
                            { field: "WebsiteName", editable: true, width: 190, title: "Project Name", validation: { required: { message: "Project name is required" } } },
                            { field: "WebsiteNotes", title: "Project Notes" },
                            { field: "WebsiteGUID", title: "Project API ID", editable: false },
                            { field: "DefaultContentType", title: "Default Content Type", width: "160px", editor: defaultContentTypeDropDownEditor, template: "#=ContentTypes.Descriptions#" },
                            { command: ["edit", "destroy"] }
                ],
                editable: "inline"
            });

            function defaultContentTypeDropDownEditor(container, options) {
                console.log(container + " : " + options);
                var dataSourcFactory = dataSourceFactory("/odata/ContentType");
                var dsContentTypes = dataSourceFactory.contentTypes();  // returns a kendo.data.DataSource() object

                $('<input required data-text-field="Description" data-value-field="ContentTypeId" data-bind="value:' + options.field + '"/>')
                    .appendTo(container)
                    .kendoDropDownList({
                        autoBind: false,
                        dataSource: dataSourceFactory.contentTypes()
                    }); // kendoDropDownList
            }
        }
    }
});

dataSourceFactory被注入到指令中,並成功用於顯示數據。

觸發行編輯時,將使用默認參數container, options調用defaultContentTypeDropDownEditor 如果我可以將dataSourceFactory傳遞給此函數, dataSourceFactory被設置,但不清楚如何從激活調用中完成此操作。

options

Object {field: "DefaultContentType", editor: function, model: n.extend.o}
editor: function defaultContentTypeDropDownEditor(container, options) {
field: "DefaultContentType"
model: n.extend.o
__proto__: Object

container

[<td role=​"gridcell" data-container-for=​"DefaultContentType">​</td>​]

如您所見, dataSourceFactory在功能級別上可見(注入到指令中),但是在defaultContentTypeDropDownEditor無法訪問。 有人可以解釋如何做到這一點嗎?

注入的對象是dataSourceFactory 通過將初始初始化重命名為dataSourceFactory1 ,並將函數回調對它的使用重命名為dataSourceFactory2 ,我能夠使它開始工作。

感謝Nikos向我指出正確的方向。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM