Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/432.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 如何在现有应用程序中添加angular js模块_Javascript_Jquery_Angularjs_Angularjs Directive_Directive - Fatal编程技术网

Javascript 如何在现有应用程序中添加angular js模块

Javascript 如何在现有应用程序中添加angular js模块,javascript,jquery,angularjs,angularjs-directive,directive,Javascript,Jquery,Angularjs,Angularjs Directive,Directive,我试图在已经存在的代码中添加一个angular JS模块。实际上,我正在尝试在其中添加一段新代码。工作流程是这样的。单击按钮时,JS将获取一个模板并将其添加到dom中。在这个添加的模板中,我想插入新的angular JS模块。我试图在模块内至少显示hello world,但没有成功 比方说,当单击一个按钮时,我正在将下面的标记添加到dom中 <div patient-details ng-controller='patientDetailsCtrl' id='patientDetails'

我试图在已经存在的代码中添加一个angular JS模块。实际上,我正在尝试在其中添加一段新代码。工作流程是这样的。单击按钮时,JS将获取一个模板并将其添加到dom中。在这个添加的模板中,我想插入新的angular JS模块。我试图在模块内至少显示hello world,但没有成功

比方说,当单击一个按钮时,我正在将下面的标记添加到dom中

<div patient-details ng-controller='patientDetailsCtrl' id='patientDetails'></div>

这是通过jquery或javascript添加的。我如何使它与以下指令一起工作

angular.module('patientDetailsModule',[])
.directive('patientDetails',function($compile){
    return{
        //restrict: 'E',
        template: "<div>Hello World!!!{{name}}</div>"
    };
});
angular.module('patientDetailsModule',[])
.directive('patientDetails',函数($compile){
返回{
//限制:'E',
模板:“你好,世界!!!{{name}”
};
});

您需要使用$compile$compile()将确保html代码段中的所有角度代码都将被编译

这里有一个例子

  • 从nc单击发送消息到指令
  • 指令接收消息,编译html并附加新元素

                var template = "<div patient-details ng-controller='patientDetailsCtrl' id='patientDetails'>Element added</div>";
                var newElement = angular.element(template);
                $compile(newElement)(scope);
                element.append(newElement);
                // can also use element.replaceWith(newElement);
    
    var template=“添加元素”;
    var newElement=angular.element(模板);
    $compile(新元素)(范围);
    元素。追加(新元素);
    //也可以使用element.replaceWith(newElement);
    
我不明白:(.你能解释一下吗?