gpt4 book ai didi

javascript - 自定义指令不显示任何 Angular js

转载 作者:行者123 更新时间:2023-11-28 07:58:47 25 4
gpt4 key购买 nike

所以我有一个自定义指令,它发出 http get 请求并显示在另一个模板上。但它不显示任何内容

我在其中创建指令的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>

和具有显示的templateUrl documentcontent.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)

内容不显示。我不确定这里出了什么问题。谢谢

最佳答案

Controller 内部指令必须以这种方式使用

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

在您的情况下,$element 是用名称 $http 注入(inject)的。当您尝试使用 $http 调用休息调用时,它实际上是在尝试调用 $element 上的 get 方法,因此您会收到 Error: [jqLit​​e : Nose ]尝试将 Controller 更改为此

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

此外,您不需要将 $http 传递到您的工厂,而是可以在工厂中定向注入(inject)

 documentDisplayModule.factory('documentDisplayAPI',function($http){

关于javascript - 自定义指令不显示任何 Angular js,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25691026/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com