2017-09-03 97 views
0

我收到以下錯誤:

(Uncaught Error: Unknown provider: $filterProvider from myapp) while using following code. Please help to solve it.

Uncaught Error: Unknown provider: $filterProvider from myapp at angular.min.js:29

HTML ---------------------

<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.0.6/angular.min.js">`</script> 
    <div ng-app="myapp" ng-controller="WeatherCtrl"> 
     <h2>Weather in Salzburg, Austria</h2> 
     <weather-icon cloudiness="{{ weather.clouds }}"></weather-icon> 
     <h3>Current: {{ weather.temp.current | temp:2 }}</h3> 
     min: {{ weather.temp.min | temp }}, max: {{ weather.temp.max | temp }} 
    </div> 

的源代碼(.js文件)-------------------

'use strict'; 

var myapp = angular.module('myapp', []); 

myapp.factory('weatherService', function($http) { 
    return { 
     getWeather: function() { 
     var weather = { temp: {}, clouds: null }; 
     $http.jsonp('http://api.openweathermap.org/data/2.5/weather?q=Salzburg,at&units=metric&callback=JSON_CALLBACK&APPID=f9dbd911bc01df1d9ce563b2ba4d3209').success(function(data) { 
      if (data) { 
       if (data.main) { 
        weather.temp.current = data.main.temp; 
        weather.temp.min = data.main.temp_min; 
        weather.temp.max = data.main.temp_max; 
       } 
       weather.clouds = data.clouds ? data.clouds.all : undefined; 
      } 
     }); 

     return weather; 
     } 
    }; 
}); 

myapp.filter('temp', function($filter) { 
    return function(input, precision) { 
     if (!precision) { 
      precision = 1; 
     } 
     var numberFilter = $filter('number'); 
     return numberFilter(input, precision) + '\u00B0C'; 
    }; 
}); 

    myapp.controller('WeatherCtrl', function ($scope, weatherService) { 
     $scope.weather = weatherService.getWeather(); 
    }); 

    myapp.directive('weatherIcon', function() { 
     return { 
      restrict: 'E', replace: true, 
      scope: { 
       cloudiness: '@' 
      }, 
      controller: funct 

ion($scope) { 
       $scope.imgurl = function() { 
        var baseUrl = 'https://ssl.gstatic.com/onebox/weather/128/'; 
        if ($scope.cloudiness < 20) { 
         return baseUrl + 'sunny.png'; 
        } else if ($scope.cloudiness < 90) { 
         return baseUrl + 'partly_cloudy.png'; 
        } else { 
         return baseUrl + 'cloudy.png'; 
        } 
       }; 
      }, 
      template: '<div style="float:left"><img ng-src="{{ imgurl() }}"></div>' 
     }; 
    }); 
+1

沒有問題! – Sajeetharan

+0

@JyotiRajSharma如果有問題,請使用此[JSFiddle](https://jsfiddle.net/0amjqtpv/)並嘗試複製問題並共享! –

回答

-1

我不認爲你可以注入$過濾器進入過濾器像你這樣做:myapp.filter('temp', function($filter)

編輯:看來我是不正確的,你應該能夠在注入$過濾

+0

檢查文檔在這裏: https://docs.angularjs.org/api/ng/service/$filter 改正你的答案。 $ filter是一個默認的角度服務。 – user3152549

相關問題