如何访问一个全球范围内的对象的角度函数内部? [英] How to access a global scope object inside an Angular function?
问题描述
在下面的控制器,我已经定义 $ scope.dataSourceFactory
。我最初有 VAR dataSourceFactory = ...
,(而不是在<$定义或以后使用它使用 $范围
C $ C> defaultContentTypeDropDownEditor()但同样的问题)。
app.controller('projectEditorController',['$范围,$日志','dataSourceFactory',
//抽象数据工厂接受REST风格的CRUD控制器的类型参数 功能($范围,$日志,dataSourceFactory){ $ scope.dataSourceFactory =新dataSourceFactory(/ ODATA / ProjectEditor); $(#电网)。kendoGrid({
数据源:$ scope.dataSourceFactory.projects()
分页:真实,
高度:400,
工具栏:创造]
列: [
{场:...,编辑:真,宽度:190,标题:姓名,验证:{要求:{消息:名称是必需的}}},
{场:DefaultContentType,标题:默认内容类型,宽度:160像素,主编:defaultContentTypeDropDownEditor,模板:#= ContentTypes.Descriptions#},
{命令:编辑,消灭]}
]
编辑:内联
}); 功能defaultContentTypeDropDownEditor(集装箱,期权){
VAR dataSourceFactory =新的$ scope.dataSourceFactory(/ ODATA /的ContentType); //错误:未捕获类型错误:对象不是一个函数
变种dsContentTypes = dataSourceFactory.contentTypes(); //返回一个kendo.data.DataSource()对象 $('&LT;输入所需的数据文本字段=描述数据值场=ContentTypeId数据绑定=值:'+ options.field +'/&GT;')
.appendTo(集装箱)
.kendoDropDownList({
autoBind:假的,
数据源:dataSourceFactory.contentTypes()
}); // kendoDropDownList
}
}]);
一旦调用编辑/制作功能, defaultContentTypeDropDownEditor()
函数被调用,这就需要利用 $ scope.dataSourceFactory <的/ code>。问题是,作为在评论中,我得到了以下错误:
未捕获类型错误:对象不是一个函数
块引用>我是pretty肯定这是一个范围的问题,但不知道如何解决。
建议?
- 更新 -
根据要求,这里是一个工厂:
app.factory('dataSourceFactory',函数(abstractDataFactory,customFunctions){
VAR的DataFactory; 功能dataSourceFactory(odataUrlBase){
的DataFactory =新abstractDataFactory(odataUrlBase);
} dataSourceFactory.prototype = {
CONTENTTYPES:功能(){
返回新kendo.data.DataSource({
...返回dataSourceFactory;
解决方案我相信你的错误行应为:
VAR dataSourceFactory =新dataSourceFactory(/ ODATA /的ContentType);
除非你的
新dataSourceFactory(/ ODATA / ProjectEditor)
返回一个函数/对象原型?此外,将是有益的看看你们的工厂code太
编辑:
要我的错误读取,无论是从线路返回
$ scope.dataSourceFactory =新dataSourceFactory(/ ODATA / ProjectEditor);
不是一个对象的原型,可以被实例化,而是一个
新kendo.data.DataSource({});
上,您可以打电话说取
命令。我有koolunix上面,你are'nt在一个非常角的方式这样做是为了达成一致。
我不是很熟悉的剑道,但我已经调查使用角剑道一>项目,你可以考虑作为替代你在你的控制器都在做JQuery的操作编辑2:
也澄清这肯定不是一个范围错误,但只是要确定你可以写你的函数为:
$ scope.defaultContentTypeDropDownEditor =功能(容器,期权){
....
}In the following controller, I have defined
$scope.dataSourceFactory
. I initially hadvar dataSourceFactory = ...
, (not using$scope
in defining or using it later indefaultContentTypeDropDownEditor()
but same issue).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 } }]);
Upon calling the edit/create function, the
defaultContentTypeDropDownEditor()
function is called, which needs to make use of the$scope.dataSourceFactory
. The problem is, as in the comments, I'm getting the following error:Uncaught TypeError: object is not a function
I'm pretty sure this is a scoping issue, but not sure how to resolve.
Suggestions?
-- UPDATE --
As requested, here's an the factory:
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;
解决方案I believe your error line should read:
var dataSourceFactory = new dataSourceFactory("/odata/ContentType");
unless your
new dataSourceFactory("/odata/ProjectEditor")
returns a function/object prototype?Alternatively it would be helpful to see your factory code too
EDIT :
To me the error reads that whatever is returned from the line
$scope.dataSourceFactory = new dataSourceFactory("/odata/ProjectEditor");
isn't an object prototype which can be instanciated but rather a
new kendo.data.DataSource({});
on which you can call say afetch
command.I have to agree with koolunix above that you are'nt doing this in a very "Angular" way. I'm not very familiar with Kendo but I have looked into using the Angular-Kendo project which you may consider as an alternative to the JQuery manipulation you are doing in you controller
Edit 2:
Also for clarification this is certainly not a scope error but just to be sure you can write your function as:
$scope.defaultContentTypeDropDownEditor = function(container, options) { .... }
这篇关于如何访问一个全球范围内的对象的角度函数内部?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!