Angularjs angular.element.scope()未定义
我试图在新的html内容中添加一个控制器,该内容将动态添加到现有dom中,但我的范围未定义。最好的方法是什么?我是新来的 对不起,这是我的代码:Angularjs angular.element.scope()未定义,angularjs,angularjs-scope,Angularjs,Angularjs Scope,我试图在新的html内容中添加一个控制器,该内容将动态添加到现有dom中,但我的范围未定义。最好的方法是什么?我是新来的 对不起,这是我的代码: var angularScript = document.createElement('script'); angularScript.setAttribute('type','text/javascript'); angularScript.setAttribute('src','https://ajax.googleapis.com/ajax/li
var angularScript = document.createElement('script');
angularScript.setAttribute('type','text/javascript');
angularScript.setAttribute('src','https://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js');
document.head.appendChild(angularScript);
jQuery('body').append("<section id='custFA'><div id='getTable' data-ng-app='custom' data-ng-controller='customController' style='overflow-y: auto !important;position:absolute; width: 300px; height: 200px; top:100px; left: 200px;'><button id='runCust' data-ng-click='codeRun()' style='top: 57px; left: 120px;'>Run</button></div></section>");
I'm inserting module and controller scripts as well, and the code in it:
Module:
setTimeout(function angularDefined(){
if(angular){
window.app = angular.module("custom",[]);
console.log("angular");
}
else{
setTimeout(angularDefined,1000);
}
},1000);
controller:
function appDefined(){
if(window.app){
window.defined=true;
console.log("appDefined");
app.controller('customController', ['$scope', function($scope) {
$scope.codeRun=function(){
console.log("check");
}
}]);
}
else{
setTimeout(appDefined,1000);
}
};
appDefined();
var angularScript=document.createElement('script');
setAttribute('type','text/javascript');
angularScript.setAttribute('src','https://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js');
文件.head.appendChild(angularScript);
jQuery('body')。追加(“Run”);
我还插入了模块和控制器脚本,以及其中的代码:
模块:
setTimeout(函数angularDefined(){
如果(角度){
window.app=angular.module(“自定义”,[]);
控制台日志(“角度”);
}
否则{
设置超时(角度定义,1000);
}
},1000);
控制器:
函数appDefined(){
如果(window.app){
window.defined=true;
console.log(“appDefined”);
app.controller('customController',['$scope',函数($scope){
$scope.codeRun=function(){
控制台日志(“检查”);
}
}]);
}
否则{
设置超时(appDefined,1000);
}
};
appDefined();
谢谢您需要先创建一个控制器。根据文档:
var myApp = angular.module('myApp',[]);
myApp.controller('GreetingController', ['$scope', function($scope) {
$scope.greeting = 'Hola!';
}]);
在html中:让视图知道要使用哪个控制器。在该div中,您可以访问$scope上的任何变量
<div ng-controller="GreetingController">
{{ greeting }}
</div>
{{问候语}
编写一些代码,然后我们可以提供帮助,您基本上描述了一个非常广阔的场景。我们确实需要更多关于您在这里尝试做什么的信息,以及您正在使用的代码。这里发生了什么?您正在使用jquery动态创建动态角度内容?你为什么要这样做?页面上没有angular(这不是我的页面),但我想用angular构建一些东西,因为我想学习它,并检查是否可以用angular创建它。我知道如何使用jQuerywell来实现这一点,但你所尝试的并没有真正意义,但如果你完全没有其他方法来包含Angular,你将不得不手动引导它,而不是使用ng应用程序。检查有关引导的文档。