在以下控制器中,我定义了$scope.dataSourceFactory
。我最初有var dataSourceFactory = ...
,(稍后在$scope
定义或使用defaultContentTypeDropDownEditor()
时没有使用app.controller('projectEditorController', ['$scope', '$log', 'dataSourceFactory',
// the abstract data factory accepts controller type parameters for RESTful CRUD
function ($scope, $log, dataSourceFactory) {
$scope.dataSourceFactory = new dataSourceFactory("/odata/ProjectEditor");
$("#grid").kendoGrid({
dataSource: $scope.dataSourceFactory.projects(),
pageable: true,
height: 400,
toolbar: ["create"],
columns: [
{ field: "...", editable: true, width: 190, title: "Name", validation: { required: { message: "Name is required" } } },
{ field: "DefaultContentType", title: "Default Content Type", width: "160px", editor: defaultContentTypeDropDownEditor, template: "#=ContentTypes.Descriptions#" },
{ command: ["edit", "destroy"] }
],
editable: "inline"
});
function defaultContentTypeDropDownEditor(container, options) {
var dataSourceFactory = new $scope.dataSourceFactory("/odata/ContentType"); // error: Uncaught TypeError: object is not a function
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
}
}]);
但是同样的问题)。
defaultContentTypeDropDownEditor()
调用edit / create函数后,调用$scope.dataSourceFactory
函数,该函数需要使用app.factory('dataSourceFactory', function (abstractDataFactory, customFunctions) {
var dataFactory;
function dataSourceFactory(odataUrlBase) {
dataFactory = new abstractDataFactory(odataUrlBase);
}
dataSourceFactory.prototype = {
contentTypes: function () {
return new kendo.data.DataSource({
...
return dataSourceFactory;
。问题是,如评论中所示,我收到以下错误:
未捕获的TypeError:对象不是函数
我很确定这是一个范围问题,但不确定如何解决。
建议?
- 更新 -
根据要求,这是一家工厂:
{{1}}
答案 0 :(得分:0)
我相信你的错误行应该是:
var dataSourceFactory = new dataSourceFactory("/odata/ContentType");
除非你的new dataSourceFactory("/odata/ProjectEditor")
返回一个函数/对象原型?
另外,查看工厂代码也很有帮助
编辑:
对我来说错误是从行
返回的内容$scope.dataSourceFactory = new dataSourceFactory("/odata/ProjectEditor");
不是可以实例化的对象原型,而是new kendo.data.DataSource({});
,您可以在其上调用fetch
命令。
我必须同意上面的koolunix,你不是以非常“角度”的方式做这件事。 我对Kendo不是很熟悉,但我已经研究过使用Angular-Kendo项目,您可以考虑将其替换为您在控制器中执行的JQuery操作
编辑2:
另外为了澄清这肯定不是范围错误,只是为了确保你可以把你的函数编写为:
$scope.defaultContentTypeDropDownEditor = function(container, options) {
....
}
答案 1 :(得分:0)
使用指令而不是jquery选择器&#39; #grid&#39;,控制器并不意味着所有这些DOM操作:
.directive('mygrid',function(dataSourceFactory) {
return {
link : function($scope,$element,$attrs) {
$element.kendoGrid({
dataSource: $scope.dataSourceFactory.projects(),
pageable: true,
height: 400,
toolbar: ["create"],
columns: [
{ field: "...", editable: true, width: 190, title: "Name", validation: { required: { message: "Name is required" } } },
{ field: "DefaultContentType", title: "Default Content Type", width: "160px", editor: defaultContentTypeDropDownEditor, template: "#=ContentTypes.Descriptions#" },
{ command: ["edit", "destroy"] }
],
editable: "inline"
});
function defaultContentTypeDropDownEditor(container, options) {
var dataSourceFactory = 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
}
}
}
});