Javascript AngularJS设置html属性
我们是JavaScript和AngularJS的新手,但是场景非常简单。我们想设置从REST服务接收的html属性 这是我们的profileController.js:Javascript AngularJS设置html属性,javascript,html,angularjs,Javascript,Html,Angularjs,我们是JavaScript和AngularJS的新手,但是场景非常简单。我们想设置从REST服务接收的html属性 这是我们的profileController.js: angular .module('app') .controller('profileController', ['$scope', '$http', '$window', function($scope, $http) { // REST service works prope
angular
.module('app')
.controller('profileController', ['$scope', '$http', '$window', function($scope, $http) {
// REST service works properly
var resource = 'http://localhost:8080/user';
$http.get(resource).then(function(result){
// logs right json
console.log(result);
// no effect
$scope.content = result.data.toString();
// no effect
if(!$scope.$$phase) {
$scope.$apply();
}
});
} ]);
profile.html
<div>
<div class="container">
<br><br><br>
<h2 class="sub-header">Profile</h2>
<div class="table-responsive">
<table class="table table-striped">
<thead>
<tr>
<th>#</th>
<th>Forename</th>
<th>Name</th>
<th>Role</th>
<th>E-Mail</th>
</tr>
</thead>
<tbody>
<tr>
<td>{{profile.data}}</td>
<!-- Doesn't work as well
<td>{{profile.forename}}</td>
<td>{{profile.lastname}}</td>
<td>{{profile.role}}</td>
<td>{{profile.email}}</td>-->
</tr>
<div id="container" ng-controller="profileController">
<content-item forename="item"></content-item>
</div>
</tbody>
</table>
</div>
</div>
</div>
轮廓
#
名字
名称
角色
电子邮件
{{profile.data}
REST服务工作正常并发送json,JavaScript控制台中的日志输出正确,只是html中的设置不起作用,我们完全没有任何线索
提前谢谢 转换
$scope.content = result.data.toString();
为此:
$scope.content = result.data;
如果你想打印里面的数据
<td>{{profile.data}}</td>
{{profile.data}
您必须更改html标记中控制器的位置。请考虑这一点
其中,上述$scope.content
是您的$scope.content=result.data的硬编码模拟代码>
使用方法:
<tr ng-repeat="profile in content">
<td>#</td>
<td>{{profile.firstname}}</td>
<td>{{profile.lastname}}</td>
<td>{{profile.role}}</td>
<td>{{profile.email}}</td>
</tr>
#
{{profile.firstname}
{{profile.lastname}
{{profile.role}
{{profile.email}
检查以获取完整的代码
PS:我将示例中的{{profile.data}
更改为#
,因为我不知道应该是什么。试试这个
检查你的ng控制器和ng应用程序,这在l中应该可以
这里,在您的代码中,ng控制器应该至少在表标记中,即
<div>
<div class="container">
<br><br><br>
<h2 class="sub-header">Profile</h2>
<div class="table-responsive">
<table class="table table-striped" ng-controller="profileController">
<thead>
<tr>
<th>#</th>
<th>Forename</th>
<th>Name</th>
<th>Role</th>
<th>E-Mail</th>
</tr>
</thead>
<tbody>
<tr>
<td>{{content}}</td>
<!-- Doesn't work as well
<td>{{content.forename}}</td>
<td>{{content.lastname}}</td>
<td>{{content.role}}</td>
<td>{{content.email}}</td>-->
</tr>
<div id="container">
<content-item forename="item"></content-item>
</div>
</tbody>
</table>
</div>
</div>
轮廓
#
名字
名称
角色
电子邮件
{{content}}
如果您收到一个用户列表,为什么要使用.toString()
呢?+1,toString()不应该在这里,您不再在这一行之后操作json对象。这只是一个演示:scope.content=result.data$scope.forename='hallo';this.lastname='lastname';那些td
s不是定义了ng controller
的div#container
的子元素。所以,“不工作”才是正确的行为。尝试将ng controller=“profileController”
移动到表
元素。
<div>
<div class="container">
<br><br><br>
<h2 class="sub-header">Profile</h2>
<div class="table-responsive">
<table class="table table-striped" ng-controller="profileController">
<thead>
<tr>
<th>#</th>
<th>Forename</th>
<th>Name</th>
<th>Role</th>
<th>E-Mail</th>
</tr>
</thead>
<tbody>
<tr>
<td>{{content}}</td>
<!-- Doesn't work as well
<td>{{content.forename}}</td>
<td>{{content.lastname}}</td>
<td>{{content.role}}</td>
<td>{{content.email}}</td>-->
</tr>
<div id="container">
<content-item forename="item"></content-item>
</div>
</tbody>
</table>
</div>
</div>