如何在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
被注入到指令中,并成功用于显示数据。
当行编辑被触发时,defaultContentTypeDropDownEditor
被调用,其默认参数为container, options
。如果我可以传递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为我指出了正确的方向。
相关文章:
- 函数参数中的数据与指定变量之间的任何性能差异
- AngularJS:我可以跳过函数参数回调吗
- 函数未将值作为参数传递
- JS:检查URL中的参数,然后迭代一个参数为var的函数
- 为什么不'我们在javascript中使用函数参数的数据类型
- 你好,这是测试用例,我必须在函数中传递n个参数
- JavaScript - 多参数函数,它是多个图像库的字符串
- 如何从两个参数函数返回随机整数
- 以无点风格在Ramda中编写一个无参数函数
- JS:将单参数函数转换为可链接函数
- 正则表达式类似于Javascript中的参数函数
- 将 $' 值传递给替换的关联参数函数
- "这个“;在参数函数中
- 如何向jquery插件发送参数函数
- 将参数函数Node.js从一个js传递到另一个js
- 如何在javascript参数函数中传递PHP post方法字符串
- 装饰 Javascript Promise.then 以便参数函数接收附加参数
- 正在分析setInterval ID'是的's参数函数
- 如何根据一个参数函数计算年龄
- 对象参数/函数和/或三元运算符混淆