动态加载输入框确实nothining [英] Dynamically loaded input box does nothining
问题描述
所以我有一些的HTML被加载到DIV动态取决于#panel上questionNumber用户上。这是不是所有的code,但所有相关的code我想到的。无论如何,<输入>
获取的加载到页面中,但它实际上并没有做任何事情。什么我在这里丢失?我有同样的问题,当questionNumber === 1,凡绑定变量只是显示为 {{}变量}
等
VAR readingController =功能(范围,使罗马){
scope.usersRomanization;
//alert(scope.usersRomanization);
}VAR应用= angular.module('教程',['功能','辅导'])。控制器('getAnswers',函数($范围,$元素,位置,使罗马){
$ scope.sectionNumber = Position.sectionNumber;
如果($ scope.sectionNumber === 0){
$('#面板)HTML。('< DIV NG控制器=readingController><输入NG模型=usersRomanization>< /输入>< / DIV>');
readingController($范围,使罗马);
}<机身NG控制器=getAnswers>
< DIV ID =面板>
< / DIV>
< /身体GT;
如果您添加HTML到DOM,你必须告诉角至$编译。这应该在一个指令来完成。你需要注入 $编译
然后做这样的事情:
VAR含量='< DIV NG控制器= ...>< / DIV>';
VAR编译= $编译(内容)(范围);
//然后把你想要内容
或者更好的,定义一个指令,并使用模板
,它会自动得到由角为您编制。
其他的替代品NG-包括(这将编译为您加载内容)和NG-开关,这样可以让你把模板到HTML。
So I have some html that gets loaded into the #panel div dynamically depending on which questionNumber the user is on. This is not all of the code but all of the relevant code I think. Anyway, the <input>
get's loaded into the page but it doesn't actually do anything. what am I missing here? I have the same problem when the questionNumber === 1, where the binded variables just show up as {{variable}}
etc
var readingController = function (scope, Romanize){
scope.usersRomanization;
//alert(scope.usersRomanization);
}
var app = angular.module('Tutorials', ['functions', 'tutorials']).controller('getAnswers', function ($scope, $element, Position, Romanize) {
$scope.sectionNumber = Position.sectionNumber;
if ($scope.sectionNumber === 0){
$('#panel').html('<div ng-controller="readingController"><input ng-model="usersRomanization"></input></div>');
readingController($scope, Romanize);
}
<body ng-controller="getAnswers">
<div id="panel">
</div>
</body>
If you add HTML to the DOM, you have to tell Angular to $compile it. This should be done in a directive. You'll need to inject $compile
then do something like this:
var content = '<div ng-controller=...></div>';
var compiled = $compile(content)(scope);
// then put the content where you want
Or better, define a directive and use a template
, which will automatically get compiled for you by Angular.
Other alternatives are ng-include (which will compile the loaded content for you) and ng-switch, which would allow you to put the templates into the HTML.
这篇关于动态加载输入框确实nothining的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!