a.html
<div ng-bind-html="htmlElement()"></div>controller.js
$scope.htmlElement = function(){
var html = '<input type="text" ng-model="myModel" />';
return $sce.trustAsHtml(html);
}但是,当我想获得文本输入的值时,
alert($scope.myModel);它说myModel是个未知数。只有当我动态添加文本输入时,才会发生这种情况。
怎样才能得到文本输入的值?
发布于 2014-09-02 06:43:49
我建议您将这个DOM操作与指令一起使用。
在你的controllers.js里
app.directive("myHtml", function(){
return {
restrict: 'E',
template: '<input type="text" ng-model="myModel" />',
link: function(scope, attr){
scope.myModel = 'Input text here';
}
}
});
app.controller('listCtrl', function($scope) {
$scope.showInputText = function(){
alert($scope.myModel);
}
});在你的HTML里
<div ng-controller="myCtrl">
<my-html><my-html>
Your input: {{myModel}}
<button ng-click="showInputText()">Show Input Text</button>
</div>发布于 2014-09-02 12:49:46
试试这个,http://plnkr.co/edit/A4HRCuTbJt21wEngH9HX?p=preview:
<div ng-bind-html="htmlElement()" compile-element></div>……
module.directive("compileElement", function($compile){
return {
link: function(scope,element) {
scope.$watch(function(){
return element.html();
}, function() {
$compile(element.contents())(scope);
});
}
};
});发布于 2014-12-30 09:43:30
尝试将以下css添加到div /元素中
white-space: pre-wrap;
word-wrap: break-word;https://stackoverflow.com/questions/25616680
复制相似问题