我试图在表格中显示文本字段(如表单)。我有一个按钮,当我点击它时,一个新行被添加到表中,所有单元格都是表单字段。当我单击按钮时,会出现一个新行(确定),但是,字段的html代码显示为文本。我尝试渲染为html,但它不起作用:
1。Render text as html with angularJs 2。AngularJS : Insert HTML into view 3。http://fdietz.github.io/recipes-with-angular-js/common-user-interface-patterns/editing-text-in-place-using-html5-content-editable.html]
我已经完成了一个index.html,它为不同的观点提供了3条路线。索引文件包含所有角度sripts和我自己的脚本。我正在寻找最简单的方法。这是我第一次使用angular。
的index.html:
<html ng-app="assets">
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script>
<script src="sources/js/angular.min.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/angular.js/1.2.10/angular-route.min.js"></script>
<script>
var assets = angular.module('assets', ['ngRoute']);
assets.config(function($routeProvider) {
$routeProvider.
when('/', {
templateUrl: 'home.html'
}).
when('/:tipusactius', {
templateUrl: 'tipusactius.html',
controller: 'TipusActiusCtrl'
}).
when('/:tipusactius/alta', {
templateUrl: 'tipusactius-alta.html',
controller: 'AfegirTipusActiusCtrl'
}).
otherwise({
redirectTo: '/'
});
});
assets.controller('TipusActiusCtrl', function ($scope, $http){
$http.get('http://10.0.203.73/WS/ws.php/tipusactius/').success(function(data) {
$scope.tipus_actius = data;
});
// Ordena taula per id desc
$scope.sortField = 'idtipus_actius';
$scope.reverse = false;
});
assets.controller('AfegirTipusActiusCtrl', function ($scope, $http, $sce){
$scope.renderHTML = "<input type='text' name='firstname'>";
// Camps formulari text pla
$scope.nomAtribut = "<input type='text' name='firstname'>";
$scope.tipus = "<input type='text' name='firstname'>";
$scope.mida = "<input type='text' name='firstname'>";
$scope.prioritat = "<input type='text' name='firstname'>";
$scope.obligatori = "<input type='text' name='firstname'>";
$scope.atributs = [];
$scope.addField = function() {
$scope.atributs.push($scope.atributs.length);
};
});
</script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css"
integrity="sha384-1q8mTJOASx8j1Au+a5WDVnPi2lkFfwwEAa8hDDdjZlpLegxhjVME1fgjWPGmkzs7" crossorigin="anonymous">
</head>
<body>
<div class="container" ng-view></div>
</body>
</html>
tipusactius-alta.html:
<script>
</script>
<div class="row">
<div class="col-md-8" ng-controller="AfegirTipusActiusCtrl">
<button ng-click="addField()">Nou atribut</button>
<div class="col-md-8">
<table class="table">
<tr>
<td>Nom atribut</td>
<td>Tipus</td>
<td>Mida</td>
<td>Prioritat</td>
<td>Obligatori</td>
</tr>
<tr ng-repeat="atribut in atributs">
<td>{{nomAtribut}}</td>
<td>{{tipus}}</td>
<td>{{mida}}</td>
<td>{{prioritat}}</td>
<td>{{obligatori}}</td>
</tr>
</table>
</div>
</div>
</div>
</div>
我整个早上都浪费了这件事。这是没有Html渲染测试的代码。任何帮助都会有所帮助。
解决:
最后我这样做了@Ankh告诉我:
文本需要由Angular编译为html才能呈现 正常。尝试创建一个&#39;编译&#39;指示。这将调用$ compile 在传递给它的任何文本..
assets.directive(&#39; compile&#39;,compile);
function compile($compile) { return { restrict: 'A', replace: true, link: linkFunction }; function linkFunction(scope, element, attrs) { scope.$watch( function (scope) { return scope.$eval(attrs.compile); }, function (value) { element.html(value); $compile(element.contents())(scope); } ); } } .. and in your template <tr ng-repeat="atribut in atributs"> <td compile="nomAtribut"></td> <td compile="tipus"></td> <td compile="mida"></td> <td compile="prioritat"></td> <td compile="obligatori"></td> </tr>
答案 0 :(得分:2)
文本需要由Angular编译为html才能正确呈现。尝试创建一个&#39;编译&#39;指示。这会在传递给它的任何文本上调用$compile
..
assets.directive('compile', compile);
function compile($compile)
{
return {
restrict: 'A',
replace: true,
link: linkFunction
};
function linkFunction(scope, element, attrs)
{
scope.$watch(
function (scope)
{
return scope.$eval(attrs.compile);
},
function (value)
{
element.html(value);
$compile(element.contents())(scope);
}
);
}
}
..并在您的模板中
<tr ng-repeat="atribut in atributs">
<td compile="nomAtribut"></td>
<td compile="tipus"></td>
<td compile="mida"></td>
<td compile="prioritat"></td>
<td compile="obligatori"></td>
</tr>