Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/459.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何访问角度函数中的全局范围对象?_Javascript_Angularjs_Angularjs Scope - Fatal编程技术网

Javascript 如何访问角度函数中的全局范围对象?

Javascript 如何访问角度函数中的全局范围对象?,javascript,angularjs,angularjs-scope,Javascript,Angularjs,Angularjs Scope,在下面的控制器中,我定义了$scope.dataSourceFactory。我最初有var dataSourceFactory=…,(在定义或稍后在defaultContentTypeDropDownEditor()中使用它时没有使用$scope,但问题相同) 我认为您的错误行应该是: var dataSourceFactory = new dataSourceFactory("/odata/ContentType"); 除非您的新数据源工厂(“/odata/ProjectE

在下面的控制器中,我定义了
$scope.dataSourceFactory
。我最初有
var dataSourceFactory=…
,(在定义或稍后在
defaultContentTypeDropDownEditor()
中使用它时没有使用
$scope
,但问题相同)


我认为您的错误行应该是:

        var dataSourceFactory = new dataSourceFactory("/odata/ContentType"); 
除非您的
新数据源工厂(“/odata/ProjectEditor”)
返回函数/对象原型

或者,也可以查看您的工厂代码

编辑:

对我来说,错误是从行中返回任何内容

$scope.dataSourceFactory = new dataSourceFactory("/odata/ProjectEditor");
不是一个可以实例化的对象原型,而是一个新的kendo.data.DataSource({})可以调用,比如一个
fetch
命令

我必须同意上面koolunix的观点,你没有以一种非常“有角度”的方式来做这件事。 我对剑道不太熟悉,但我已经研究过使用这个项目,你可以把它看作是你在控制器

中所做的jQuery操作的一种选择。 编辑2:

另外,为了澄清,这当然不是范围错误,只是为了确保您可以将函数编写为:

    $scope.defaultContentTypeDropDownEditor = function(container, options) {
        ....
    }

使用指令而不是jquery选择器“#grid”,控制器并不适用于所有这些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
        }
        }
    }
});
指令('mygrid',函数(dataSourceFactory){ 返回{ 链接:函数($scope、$element、$attrs){ $element.kendoGrid({ 数据源:$scope.dataSourceFactory.projects(), pageable:对, 身高:400, 工具栏:[“创建”], 栏目:[ {字段:“…”,可编辑:true,宽度:190,标题:“名称”,验证:{必需:{消息:“名称是必需的”}}, {字段:“DefaultContentType”,标题:“默认内容类型”,宽度:“160px”,编辑器:defaultContentTypeDropDownEditor,模板:“#=ContentTypes.Descriptions#”}, {命令:[“编辑”、“销毁”]} ], 可编辑:“内联” }); 函数defaultContentTypeDropDownEditor(容器,选项){ var dataSourceFactory=dataSourceFactory(“/odata/ContentType”); var dsContentTypes=dataSourceFactory.contentTypes();//返回一个kendo.data.DataSource()对象 $('') .appendTo(容器) .kendoDropDownList({ 自动绑定:错误, dataSource:dataSourceFactory.contentTypes() });//kendoDropDownList } } } });
更新了这个问题。我最初尝试使用
var
,但问题相同。同时输入数据工厂代码以供参考。感谢您的建议。我将其移动到指令中,但仍然遇到相同的问题,即无法从
defaultCOntentTypeDropDownEditor()
函数内部访问注入的
dataSourceFactory
。我删除了指令,现在在视图代码的右侧定义了网格,但遇到了问题。见:
    $scope.defaultContentTypeDropDownEditor = function(container, options) {
        ....
    }
.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
        }
        }
    }
});