将数据从服务传递到AngularJs中的指令

时间:2015-05-05 06:28:39

标签: angularjs service controller directive

这是我的指令代码

app.directive('studentlist',function(){
return{
    restrict:'E',
    scope: {},
    templateUrl:'js/directives/studentList.html'
};

});

这是我服务的代码

app.factory('students',['$http',function($http){
return $http.get('http://localhost/database2/php/students.php')
    .success(function(data){
        return data;
    })
    .error(function(err){
        return err;
    });

}]);

这是我的HTML片段

<div ng-repeat="student in studentList" class="row border">
        <studentList info="{{ students.info }}"></studentlist>
</div>

我需要知道如何将数据从服务传递到指令。所以它出现在标签

1 个答案:

答案 0 :(得分:1)

app.directive('studentlist',function(students){
return{
    restrict:'E',
    scope: {},
    link: function(scope) {
      students.then(data) {
        console.log('putting "students" on scope!:', data);
        scope.students = data;
      };
    },
    templateUrl:'js/directives/studentList.html'
};

不确定您的JSON是什么样的,但您的指令最终应该可以访问学生blob。尝试将{{students}}放在studentList指令模板中。

...而且你不再需要在指令属性中绑定。

<div ng-repeat="student in studentList" class="row border">
        <studentList></studentlist>
</div>