Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/449.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/24.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 AngularJS工厂在外部js上调用新_Javascript_Angularjs_Service_Factory - Fatal编程技术网

Javascript AngularJS工厂在外部js上调用新

Javascript AngularJS工厂在外部js上调用新,javascript,angularjs,service,factory,Javascript,Angularjs,Service,Factory,我正在我的项目中使用AceEditor JavaScript库。我想通过角度服务使用它,而不是通过调用new 而不是在我的控制器中: $scope.ace = new AceEditor('my-editor-div'); 我已经为一项服务创建了一个工厂: myapp.factory('AceService', function() { this.init=function(name) { return new AceEditor(name); };

我正在我的项目中使用AceEditor JavaScript库。我想通过角度服务使用它,而不是通过调用
new

而不是在我的控制器中:

$scope.ace = new AceEditor('my-editor-div');
我已经为一项服务创建了一个工厂:

myapp.factory('AceService', function() {

    this.init=function(name) {
        return new AceEditor(name);
    };

    return this;
});
我这样调用控制器:

$scope.ace = AceEditorService.init('my-editor-div');
YOUR_MODULE.directive("ace",[function() {
return {
    restrict : 'EA',
    require : '?ngModel',
    replace:true,
    template:"<div></div>",
    scope:{
    },
    link : function(scope, elem, attrs, ngModel) {
        if(!ngModel) 
            return;

        //bind ace editor to editor attribute
        scope.$watch('ace', function() {
            if(attrs.editor)
                scope.$eval('$parent.'+attrs.editor + ' = ace');
        });


        var html = elem.html();
        var editor = ace.edit(elem[0]);

        //some default settings
        editor.setTheme("ace/theme/chrome");
        editor.getSession().setMode(attrs.mode ? "ace/mode/"+attrs.mode : "ace/mode/javascript");
        attrs.$observe("mode",function(v){
            if(v){
                editor.getSession().setMode("ace/mode/"+attrs.mode);
            }
        });

        editor.resize();
        editor.setValue(html);
        editor.clearSelection();
        editor.moveCursorTo(0, 0);

        scope.ace = editor;

        //specify how UI should be updated
        ngModel.$render = function() {
            editor.setValue(ngModel.$viewValue);
        };

        function valid(editor) {
            return (Object.keys(editor.getSession()
                    .getAnnotations()).length == 0);
        }
        editor.on('change', function() {
            ngModel.$setViewValue(editor.getValue());
        });
    }
};
}]);
<ace ng-model="some_value" mode="xml"></ace>

我用正确的角度来做这件事吗

您可以使用第三方指令,如

或者,您也可以像下面这样轻松地使用angular指令包装Ace编辑器:

$scope.ace = AceEditorService.init('my-editor-div');
YOUR_MODULE.directive("ace",[function() {
return {
    restrict : 'EA',
    require : '?ngModel',
    replace:true,
    template:"<div></div>",
    scope:{
    },
    link : function(scope, elem, attrs, ngModel) {
        if(!ngModel) 
            return;

        //bind ace editor to editor attribute
        scope.$watch('ace', function() {
            if(attrs.editor)
                scope.$eval('$parent.'+attrs.editor + ' = ace');
        });


        var html = elem.html();
        var editor = ace.edit(elem[0]);

        //some default settings
        editor.setTheme("ace/theme/chrome");
        editor.getSession().setMode(attrs.mode ? "ace/mode/"+attrs.mode : "ace/mode/javascript");
        attrs.$observe("mode",function(v){
            if(v){
                editor.getSession().setMode("ace/mode/"+attrs.mode);
            }
        });

        editor.resize();
        editor.setValue(html);
        editor.clearSelection();
        editor.moveCursorTo(0, 0);

        scope.ace = editor;

        //specify how UI should be updated
        ngModel.$render = function() {
            editor.setValue(ngModel.$viewValue);
        };

        function valid(editor) {
            return (Object.keys(editor.getSession()
                    .getAnnotations()).length == 0);
        }
        editor.on('change', function() {
            ngModel.$setViewValue(editor.getValue());
        });
    }
};
}]);
<ace ng-model="some_value" mode="xml"></ace>
YOUR_MODULE.directive(“ace”,[function(){
返回{
限制:“EA”,
要求:“?ngModel”,
替换:正确,
模板:“”,
范围:{
},
链接:功能(范围、要素、属性、ngModel){
如果(!ngModel)
返回;
//将ace编辑器绑定到编辑器属性
范围:$watch('ace',function(){
如果(属性编辑器)
作用域.$eval(“$parent.+attrs.editor+'=ace”);
});
var html=elem.html();
var editor=ace.edit(元素[0]);
//一些默认设置
编辑器.setTheme(“ace/theme/chrome”);
editor.getSession().setMode(attrs.mode?“ace/mode/”+attrs.mode:“ace/mode/javascript”);
属性$观察(“模式”,功能(v){
如果(v){
editor.getSession().setMode(“ace/mode/”+attrs.mode);
}
});
editor.resize();
setValue(html);
编辑:();
编辑器.moveCursorTo(0,0);
scope.ace=编辑器;
//指定应如何更新UI
ngModel.$render=function(){
编辑器.setValue(ngModel.$viewValue);
};
函数有效(编辑器){
return(Object.keys(editor.getSession())
.getAnnotations())。长度==0);
}
关于('change',function()){
ngModel.$setViewValue(editor.getValue());
});
}
};
}]);
然后在html中调用它,如下所示:

$scope.ace = AceEditorService.init('my-editor-div');
YOUR_MODULE.directive("ace",[function() {
return {
    restrict : 'EA',
    require : '?ngModel',
    replace:true,
    template:"<div></div>",
    scope:{
    },
    link : function(scope, elem, attrs, ngModel) {
        if(!ngModel) 
            return;

        //bind ace editor to editor attribute
        scope.$watch('ace', function() {
            if(attrs.editor)
                scope.$eval('$parent.'+attrs.editor + ' = ace');
        });


        var html = elem.html();
        var editor = ace.edit(elem[0]);

        //some default settings
        editor.setTheme("ace/theme/chrome");
        editor.getSession().setMode(attrs.mode ? "ace/mode/"+attrs.mode : "ace/mode/javascript");
        attrs.$observe("mode",function(v){
            if(v){
                editor.getSession().setMode("ace/mode/"+attrs.mode);
            }
        });

        editor.resize();
        editor.setValue(html);
        editor.clearSelection();
        editor.moveCursorTo(0, 0);

        scope.ace = editor;

        //specify how UI should be updated
        ngModel.$render = function() {
            editor.setValue(ngModel.$viewValue);
        };

        function valid(editor) {
            return (Object.keys(editor.getSession()
                    .getAnnotations()).length == 0);
        }
        editor.on('change', function() {
            ngModel.$setViewValue(editor.getValue());
        });
    }
};
}]);
<ace ng-model="some_value" mode="xml"></ace>

与具有UI的第三个组件集成,使用指令。