2014-03-14 40 views
0

指令:什麼是錯,曾首次

'use strict'; 
var foodMeApp = angular.module('foodMeApp', ['ngResource', 'breeze.angular.q']); 

foodMeApp.directive('angularReleases', function(){ 
    return { 
    restrict :'EA', 
    link : function($scope, $http){ 
     $http.get('http://api.github.com/repos/angular/angular.js/commits') 
      .success(function(commits) { 
       $scope.commits = commits 
      }) 

    }, 
    template : '<ul>'+ 
       '<li ng-repeat="commit in commits">'+ 
       '{{ commit.commit.committer.date | date }}'+ 
       '<a ng-href="https://github.com/angular/angular.js/commit/{{commit.sha}}">{{ commit.sha }}</a>'+ 
       '{{ commit.commit.message }}'+ 
       '</li>'+ 
       '</ul>' 
    } 

}); 

,並考慮:

<angular-releases /> 

而且文件包含在後角中的index.html文件。 js文件,它第一次工作,之後,它顯示錯誤:

enter image description here

任何機構還可以建議我一些關於如何分析/回溯錯誤的技巧/提示,就像這個錯誤我只能理解'$ http.get(...)是未定義的'。休息別人我不知道。

回答

1

你應該注入$ HTTP到指令

foodMeApp.directive('angularReleases',['$http', function($http){ 
    return { 
    link : function($scope){ 
    },  
}]); 

您的主要問題是,鏈接功能的第二個參數是element,這沒有get方法。

鏈路接受一個函數具有以下簽名,function link(scope, element, attrs) { ... }其中:

  • scope是角範圍對象。
  • element是此指令匹配的jqLit​​e-wrapped元素。
  • attrs是一個具有標準化屬性名稱及其相應屬性值的鍵值對的哈希對象。
+0

是的你是對的,我忘了鏈接函數的語法,非常感謝 –

1

你需要注入$ HTTP到指令:

foodMeApp.directive('angularReleases', function($http){