【发布时间】:2015-02-17 21:22:33
【问题描述】:
假设我正在使用 $http 将变量加载到 $scope:
$http.get('/teachers/4').success(function(data){
$scope.teacher = data;
});
我的模板使用了这些数据:
Teacher: {{teacher.name}}
<students-view students="teacher.students"></students-view>
该指令可以在教师完成加载之前加载,但我的指令的代码取决于正在加载的teacher.students 数组:
app.directive('studentsView', function(){
return {
scope: { students: '=' },
controller: function($scope){
_.each($scope.students, function(s){
// this is not called if teacher loads after this directive
});
}
};
});
如何在此处获得我想要的行为?我不想停止使用 $http,并且如果可能的话,我希望不必为范围分配承诺。
【问题讨论】:
-
循环中发生了什么?如果有帮助,可以将该循环放入控制器中的成功回调中,或者将整个请求移至服务
标签: angularjs angularjs-directive