输入仅显示为html平面,而不显示为angularjs输入材料。我找不到解决我问题的确切方法
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.7.6/angular.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.7.6/angular-animate.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.7.6/angular-aria.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.7.6/angular-messages.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angular_material/1.1.12/angular-material.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.7.6/angular-sanitize.js"></script>
<div ng-app="myApp" ng-controller="myCntrl">
<div ng-bind-html="text1"></div>
</div>
<script>
angular.module('myApp', ['ngMaterial', 'ngMessages', 'ngSanitize', 'ngAria'
])
.controller('myCntrl',function($scope,$sce){
var text1=$sce.trustAsHtml("<md-input-container class='md-block' flex-gt-sm><label>To Location</label>"+
"<input required name='name' ng-model='user.name'></md-input-container>");
$scope.text1=text1;
})
</script>
答案 0 :(得分:1)
这正在工作。
<html>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/angular-material/1.1.12/angular-material.css" />
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.7.6/angular.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.7.6/angular-animate.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.7.6/angular-aria.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.7.6/angular-messages.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angular_material/1.1.12/angular-material.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.7.6/angular-sanitize.js"></script>
<script>
angular.module('myApp', ['ngMaterial', 'ngMessages', 'ngSanitize'
])
.controller('myCntrl',function($scope,$sce){
var text1=$sce.trustAsHtml("<md-input-container class='md-block' flex-gt-sm><label>To Location</label>"+
"<input required name='name' ng-model='user.name'></md-input-container>");
$scope.text1=text1;
}).directive('bindHtmlCompile', ['$compile', function ($compile) {
return {
restrict: 'A',
link: function (scope, element, attrs) {
scope.$watch(function () {
return scope.$eval(attrs.bindHtmlCompile);
}, function (value) {
element.html(value);
$compile(element.contents())(scope);
});
}
};
}]);
</script>
<body>
<div ng-app="myApp" ng-controller="myCntrl">
<div bind-html-compile="text1"></div>
</div>
</body>
</html>
答案 1 :(得分:0)
您可以通过这种方式从控制器绑定html,但是从正确的方式从控制器(model
)绑定ng-model='user.name'
控制器
angular.module('myApp', ['ngMaterial', 'ngMessages', 'ngSanitize', 'ngAria'
])
.controller('myCntrl',function($scope,$sce){
var text1="<md-input-container class='md-block' flex-gt-sm><label>To Location</label>"+
"<input required name='name' ng-model='user.name'></md-input-container>";
$scope.text1=text1;
})
查看
<div ng-app="myApp" ng-controller="myCntrl">
<div ng-bind-html="trustAsHtml(text1)"></div>
</div>
答案 2 :(得分:0)
我找到了没有指令的更简单的解决方案。
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/angular-material/1.1.12/angular-material.css" />
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.7.6/angular.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.7.6/angular-animate.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.7.6/angular-aria.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.7.6/angular-messages.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angular_material/1.1.12/angular-material.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.7.6/angular-sanitize.js"></script>
<script>
angular.module('myApp', ['ngMaterial', 'ngMessages', 'ngSanitize'
])
.controller('myCntrl',function($scope,$sce,$compile){
var mc=this;
mc.addInput=function(){
var html="<md-input-container class='md-block' flex-gt-sm><label>To Loon</label>"+
"<input required name='name' ></md-input-container>";
angular.element(document.getElementById('value')).append($compile(html)($scope));
}
});
</script>
<body>
<div ng-app="myApp" ng-controller="myCntrl as mc">
<button ng-click="mc.addInput()">ClickHere</button>
<div id="value" ></div>
</div>
</body>