所以我在服务器上生成表单HTML,并通过API将其提供给angular使用。原因是表单需要由服务器端插件生成。对于Angular来说,这可能不是最好的方法,但我想知道这是否可能。。。
template.html
<form>
<div ng-bind-html="form"></div>
<button ng-click="save"></button>
</form>
directive.js(节略)
ExtensionManagementService.getConfiguration({
extension_id: $scope.extension.id,
configuration_id: configuration.id || null
}).$promise.then(function(data) {
$scope.form = $sce.trustAsHtml(data['form']);
$scope.configuration = data.data;
})
上面的代码成功地绑定到div中,我可以看到我所期望的表单。
示例标记:
<p>
<label for="id_name">Name:</label>
<input id="id_name" name="name" ng-model="configuration.name" type="text" />
</p>
我有一个保存事件,它将scope.config传递到一个控制器中,然后我通过控制台输出值。
但是configuration.name总是空的,我想是因为angular还没有注册插入标记的绑定。
有没有一种方法可以本质上推动Angular?
正如@originof所建议的那样,$compile
模块是关键:
ExtensionManagementService.getConfiguration({
extension_id: $scope.extension.id,
configuration_id: configuration.id || null
}).$promise.then(function(data) {
var form_element = $element.find('div[role="form"]');
form_element.html(data['form']);
$scope.configuration = data.data;
$compile(form_element.contents())($scope);
});