gpt4 book ai didi

angularjs - 扩展文本过滤器

转载 作者:行者123 更新时间:2023-12-02 03:18:03 25 4
gpt4 key购买 nike

我在这里尝试扩展标准文本过滤器以执行获取请求并将值传递给已输入的扩展过滤器

过滤器名称是“搜索”:

myapp.filter('search', function($filter){    
console.log('search param'+$scope.search)
$http.get('http-hello2.html').success(function (data) {
return $filter;
});

但是收到错误:

Error: [$http:badreq] http://errors.angularjs.org/1.4.9/$http/badreq?p0=undefined
at Error (native)
at https://ajax.googleapis.com/ajax/libs/angularjs/1.4.9/angular.min.js:6:416
at m (https://ajax.googleapis.com/ajax/libs/angularjs/1.4.9/angular.min.js:85:218)
at Function.c.$get.m.(anonymous function) [as get] (https://ajax.googleapis.com/ajax/libs/angularjs/1.4.9/angular.min.js:90:109)
at link (https://run.plnkr.co/rz2TWpQpyYaVbHXN/script.js:14:27)
at https://ajax.googleapis.com/ajax/libs/angularjs/1.4.9/angular.min.js:73:222
at ca (https://ajax.googleapis.com/ajax/libs/angularjs/1.4.9/angular.min.js:73:279)
at I (https://ajax.googleapis.com/ajax/libs/angularjs/1.4.9/angular.min.js:62:174)
at https://ajax.googleapis.com/ajax/libs/angularjs/1.4.9/angular.min.js:69:193
at https://ajax.googleapis.com/ajax/libs/angularjs/1.4.9/angular.min.js:119:221 <status-viewer url="sourceUrl" class="ng-isolate-scope">

如何扩展标准 AngularJS 文本过滤器以调用自定义功能并将参数传递给此过滤器?自定义功能应在调用标准过滤器逻辑之前发生。

计划:https://plnkr.co/edit/F0XsOPZKq5HArFo9vtFs?p=preview

来源:

goob.html : 
goob

http-hello2.html
2. http-hello2.html

test.html :
test

index.html :
<!doctype html>
<html ng-app="app">
<head>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.9/angular.min.js"></script>
<script src="script.js"></script>
</head>
<body>

<div ng-controller="FetchCtrl">

<label>Filter: <input ng-model="search"></label>


<div ng-repeat="sourceUrl in sourceUrls | filter:search">
<status-viewer url="sourceUrl"> </status-viewer>
</div>
</div>


</body>
</html>

mytemplate.html :

<!--<h1>{{url}}</h1>-->
<div>
<p>{{model}}</p>

</div>

script.js :
var myapp = angular.module('app', []).controller('FetchCtrl', FetchCtrl)

myapp.directive('statusViewer', function ($http , $interval) {
return {
restrict: 'E',
templateUrl: 'mytemplate.html',
scope: {
url: '='
},
link: function (scope, elem, attrs, ctrl) {

scope.isFinishedLoading = false;

$http.get(scope.url).success(function (data) {
scope.model = data;
});
}
};
});

myapp.filter('search', function($filter){
console.log('search param'+$scope.search)
$http.get('http-hello2.html').success(function (data) {
return $filter;
});

});

function FetchCtrl($scope, $http, $q , $parse) {


$scope.sourceUrls = [
'http-hello2.html',
,'test.html'
,'goob.html'];



}

最佳答案

首先,您需要将 $http 服务注入(inject)您的过滤器以防止出现错误:

myapp.filter('search', function($filter, $http){    

第二件事:

你不应该做的

$http.get(scope.url)

不确定 scope.url 是否已经设置。我建议按条件做请求;

 if(scope.url){
//$http.get(scope.url)...
}

关于angularjs - 扩展文本过滤器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35390801/

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