1

所以我有一個自定義指令,使http獲取請求並顯示在另一個模板上。但它不會顯示任何內容自定義指令不顯示任何角度js

documentdisplay.js文件,其中我已經創建了指令

/** 
* @author Karan Shah 
* 
*/ 

var documentDisplayModule = angular.module('ald.documentdisplay',[]); 


documentDisplayModule.factory('documentDisplayAPI',function(){ 
    var fac ={}; 

    fac.getContents = function($http,docName){ 
     return $htp({ 
      method: 'GET', 
      url: "/document/getContents/docName?="+docName 
     }); 
    }; 
    return fac; 

}); 


documentDisplayModule.directive('doccontent', function() { 
    return { 
     restrict: 'EA', 
     scope: {}, 
     replace: true, 
     link: function ($scope, element, attributes) { 
     }, 
     controller: function ($scope, $http,$routeParams, documentDisplayAPI) { 

      documentDisplayAPI.getContents($http,$routeParams.doc).success(function(data,status){ 

       $scope.textofdocument = data; 
      }).error(function(data,status){ 
       if (404==status){ 
        alert("no text"); 
       } else { 
        alert("bad stuff!"); 
       } 
      });    
     }, 
     templateUrl: "documentcontent.html" 
    }; 
}); 

html頁面在那裏,我叫它

<!-- @author Karan Shah --> 
<body> 
    <div> 
     <h1>Document Display Contents</h1> 
     <doccontent/> 


    </div> 
</body> 

templateUrldocumentcontent.html其中有顯示

<body> 
<div> 
    Hello World 
    <div> 
     <a href="#/documents">Back to the List of Documents</a> 
    </div> 
    <br>  
    {{textofdocument}} 
</div> 
</body> 

錯誤,我越來越:

Error: [jqLite:nosel] Looking up elements via selectors is not supported by jqLite! See: http://docs.angularjs.org/api/angular.element 
http://errors.angularjs.org/1.2.21/jqLite/nosel 
    at https://ajax.googleapis.com/ajax/libs/angularjs/1.2.21/angular.js:78:12 
    at JQLite (https://ajax.googleapis.com/ajax/libs/angularjs/1.2.21/angular.js:2365:13) 
    at https://ajax.googleapis.com/ajax/libs/angularjs/1.2.21/angular.js:6816:27 
    at https://ajax.googleapis.com/ajax/libs/angularjs/1.2.21/angular.js:8074:11 
    at deferred.promise.then.wrappedCallback (https://ajax.googleapis.com/ajax/libs/angularjs/1.2.21/angular.js:11520:81) 
    at deferred.promise.then.wrappedCallback (https://ajax.googleapis.com/ajax/libs/angularjs/1.2.21/angular.js:11520:81) 
    at https://ajax.googleapis.com/ajax/libs/angularjs/1.2.21/angular.js:11606:26 
    at Scope.$RootScopeProvider.$get.Scope.$eval (https://ajax.googleapis.com/ajax/libs/angularjs/1.2.21/angular.js:12632:28) 
    at Scope.$RootScopeProvider.$get.Scope.$digest (https://ajax.googleapis.com/ajax/libs/angularjs/1.2.21/angular.js:12444:31) 
    at Scope.$RootScopeProvider.$get.Scope.$apply (https://ajax.googleapis.com/ajax/libs/angularjs/1.2.21/angular.js:12736:24) 

不顯示內容。我不確定這裏有什麼問題。由於

+0

有在工廠一個錯字:'返回$ htp' – Malk 2014-09-05 17:34:47

+0

我認爲你不應該使用$ HTTP一個指令內。指令是爲了操縱DOM。要從服務器中檢索數據,或將數據發送到服務器,請在控制器或服務器中使用$ http。 – Callebe 2014-09-05 17:39:07

+0

以及咒語更正沒有它運行它makae。我在控制器內部的指令中有$ http。我有另一個頁面可以在相同的情況下正常運行。不知道爲什麼這個不工作。 – 2014-09-05 17:51:51

回答

0

控制器內部指令必須以這種方式

controller: function($scope, $element, $attrs, $transclude, otherInjectables) { ... } 

被用來在你的情況$element被注入名稱$http。當您嘗試使用$http它實際上是試圖調用get方法上$element,因此你越來越Error: [jqLite:nosel] 嘗試將控制器更改爲此

controller: function($scope, $element, $attrs, $transclude, $http, $routeParams, documentDisplayAPI) { ... } 

調用REST調用你也不必通過$ HTTP到你的工廠,而不是您可以直接注射在工廠

documentDisplayModule.factory('documentDisplayAPI',function($http){ 
相關問題