来自API的Angular Form HTML,其中ng-model不绑定

时间:2014-10-07 12:50:14

标签: angularjs

因此,我在服务器上生成表单HTML,并通过API提供角度以供使用。原因是表单需要由服务器端插件生成。对于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>

我有一个save事件,它将scope.configuration传递给一个控制器,然后我控制掉这些值。

然而,configuration.name总是空白的,我希望因为angular没有注册插入标记的绑定。

有没有办法基本上给Angular一个轻推?

1 个答案:

答案 0 :(得分:0)

正如@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);
});