2015-05-05 23 views
0

這裏我的代碼指令從服務傳遞數據的指令在AngularJs

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> 

我需要知道如何從服務到指令傳遞數據。所以它顯示在標籤

+0

感謝您的反饋,我的JSON看起來是這樣的標識: 「2015552117」 姓: 「失讀症」 中間名: 「吉諾」 名字: 「貝拉」 性別: 「男」 出生日期:「 2015年4月30" 日 狀態: 「阿布賈」 YearEnrolled: 「0000」 EducationalLevel: 「JS 2」 LGA: 「特馬」 狀態: 「」 –

回答

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是什麼樣子,但你的指令應該最終能夠獲得學生的斑點。嘗試將{{students}}放入studentList指令模板中。

...並且您不再需要指令屬性中的綁定。

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

感謝您的反饋,但是我得到的對象數組,但他們不顯示數據,只是HTML。我在我的指令中使用了{{student.FirstName}},這取決於我的json對象是如何構造的 –

相關問題