gpt4 book ai didi

javascript - AngularJS : directive does not update scope after $http response in parent scope

转载 作者:行者123 更新时间:2023-11-28 08:07:23 25 4
gpt4 key购买 nike

我得到了这个指令:

.directive('studentTable', [function() {
return {
restrict: 'A',
replace: true,
scope: {
students: "=",
collapsedTableRows: "="
},
templateUrl: 'partials/studentTable.html',
link: function(scope, elem, attrs) {
...
}
}
}

模板:

  <table class="table">
<thead>
<tr>
<th><b>Name</b></th>
<th><b>Surname</b></th>
<th><b>Group</b></th>
</tr>
</thead>
<tbody>
<tr ng-repeat="student in students track by $index">
<td>{{ student.name }}</td>
<td>{{ student.surname }}</td>
<td>{{ student.group }}</td>
</tr>
</tbody>
</table>

在我的 html 中使用指令,如下所示:

<div student-table students="students" 
collapsedTableRows="collapsedTableRows"></div>

以及父 Controller :

.controller('SchoolController', ['$scope', 'User', function($scope, User){
$scope.students = [];
$scope.collapsedTableRows = [];

$scope.search = function(value) {
if(value) {
var orgId = $state.params.id;
var search = User.searchByOrg(orgId, value);

search.success(function (data) {
$scope.students = data;
$scope.collapsedTableRows = [];
_(data).forEach(function () {
$scope.collapsedTableRows.push(true);
});

});
}
}
}])

现在一开始,表是空的,因为学生数组中没有用户。单击搜索并获取学生对象列表后,我将它们放入作用域变量中,但指令没有更新,也没有在模型中找到更改(scope.$watch('students',...).我缺少什么?

附注如果我使用 $httpBackend 模拟数据,指令将按预期工作。

最佳答案

请确保数据对象返回学生数组,因为有时您必须使用 data.data,简单的演示应该可以帮助您:

http://plnkr.co/edit/UMHfzD4oSCv27PnD6Y6v?p=preview

 $http.get('studen.json').then(function(students) {

$scope.students = students.data; //<-students.data here

},
function(msg) {
console.log(msg)
})

关于javascript - AngularJS : directive does not update scope after $http response in parent scope,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24672336/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com