所以我有一些 html 会根据用户所在的 QuestionNumber 动态加载到 #panel div 中。这不是全部代码,而是我认为的所有相关代码。无论如何,<input>
get 已加载到页面中,但实际上并没有执行任何操作。我在这里缺少什么?当 QuestionNumber === 1 时,我遇到同样的问题,其中绑定变量仅显示为{{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>
如果你将 HTML 添加到 DOM,你必须告诉 Angular 来 $compile 它。这应该在指令中完成。你需要注射$compile
然后做这样的事情:
var content = '<div ng-controller=...></div>';
var compiled = $compile(content)(scope);
// then put the content where you want
或者更好的是,定义一个指令并使用template
,Angular 会自动为您编译它。
其他替代方案是 ng-include (它将为您编译加载的内容)和 ng-switch,它允许您将模板放入 HTML 中。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)