Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/373.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 - Fatal编程技术网

在视图中执行javascript代码

在视图中执行javascript代码,javascript,angularjs,Javascript,Angularjs,我正在使用AngularJS,我有一个索引页面,在这个页面中我有一个。 当我键入一些url(例如#/offers/new时,我调用一个页面,例如newoffer.html,该页面将被注入ui视图中 我在index.html页面中调用了一个javascript代码块,在这个javascript代码中,我想做一些html处理,我想处理的html代码在'new-offer.html页面中 下面是发生的情况,当我加载index.html时,它执行javascript代码,但在index.html中找不到

我正在使用AngularJS,我有一个索引页面,在这个页面中我有一个
。 当我键入一些url(例如
#/offers/new
时,我调用一个页面,例如
newoffer.html
,该页面将被注入
ui视图中

我在
index.html
页面中调用了一个javascript代码块,在这个javascript代码中,我想做一些html处理,我想处理的html代码在'new-offer.html页面中

下面是发生的情况,当我加载
index.html
时,它执行javascript代码,但在index.html中找不到要处理的html代码,然后当我调用
#/offers/new
时,
new offer.html
将被注入,不会发生任何事情

这是我的index.html:

<!doctype html>
<html>
...

  <div ui-view></div>

  ... Code
  ... Other scripts
<script src="scripts/app.js"></script>
<!--TEXT EDITOR JS-->
 <script src="scripts/js/editor.js"></script>
    </body>

</html>
这是我在
index.html
中调用的脚本,我希望在调用
#/offers/new
时执行它:

//TEXT EDITOR
    if ($('.txtEditor').length) {
    $(".txtEditor").Editor();
    }
如果不在
newoffer.html
页面中调用javascript代码,是否就可以解决这个问题?我的意思是在我将
newoffer.html
中的html代码注入
index.html
之前不执行javascript代码

编辑1: 我尝试使用以下指令:

在app.js中:

.directive('textEditordir', function() {
  return {
    template: '<div class="text-editor-box"><textarea class="txtEditor"></textarea></div>'
  };
});

但是,javascript代码仍然不起作用

您可以像下面那样为此创建指令-

app.directive('editor', function(){
 return{
     restrict:'A',
     link:function(scope,element,attr)
             {
              $(element).Editor();
             }

}

});
现在,您可以在html上使用它作为-

 <textarea class="txtEditor" editor></textarea>


您没有提到代码的作用,但一般来说-如果您想在AngularJS中使用DOM,您可能应该使用指令。在这里阅读:@AdirAmsalem请检查我所做的修改一般来说,JQuery是开箱即用的。尝试angular.element创建自己的DOM操作。您正在尝试的可能不是直接可能的。尝试route、config definitionsTry resolve、OneNet、onExit等中的指令或替代方法。。。看到编辑了。你有错误吗?我试过了,发现$不是函数,即使我在调用app.jstry之前声明了jQuery,但没有使用$符号,比如as-element.Editor();当jQuery.js加载到angular.js之前的页面时,无需使用
$(element)
元素
将已经是jQuery对象
app.directive('editor', function(){
 return{
     restrict:'A',
     link:function(scope,element,attr)
             {
              $(element).Editor();
             }

}

});
 <textarea class="txtEditor" editor></textarea>