filter $ scope错误:未知提供者:$ scopeProvider< - $ scope< - transformSensorStatusFilter

时间:2013-10-11 05:49:35

标签: javascript angularjs filter controller scope

marsApp.filter("transformSensorStatus", function($scope) {
    return function(input, options) {

    var sensorReading = ( input ? ( input / 1000) : 0);
    var sensorLowLimit = (options["lowLimit"] ? (options["lowLimit"] / 1000) : 0);
    var sensorHighLimit = (options["highLimit"] ? (options["highLimit"] / 1000) : 0);
    var curStat;
    switch (sensorReading) {
        case 255:
        case 254:
        case 253:
            curStat = generateStateInnerHtml(sensorReading);
            break;
        default:
            curStat = generateStateInnerHtml(options["sensorStatus"]);

    }

    return curStat;


    function generateStateInnerHtml(state) {

        var stateHtml = null;

        if (state == 255 || state == 254) {
            stateHtml = "a";
            $scope.sensorStateColor='';
            return state_html;
        }
        if (state == 253) {
            stateHtml = "b";
            $scope.sensorStateColor="text-warning";
            return state_html;
        }
        if (state >= 0x20) {
            stateHtml = "c";
            $scope.sensorStateColor="text-error";
            return stateHtml;
        }
        if (state >= 0x02) {
            stateHtml = "d";
            $scope.sensorStateColor="text-error";
            return stateHtml;
        }
        if (state == 0x01) {
            stateHtml = "e";
            $scope.sensorStateColor="text-success";
            return stateHtml;
        }
        stateHtml = "N/A";
        return stateHtml;
    }
}

});

在chrome中

,我收到以下错误:

  

错误:未知提供者:$ scopeProvider< - $ scope< -   transformSensorStatusFilter

2 个答案:

答案 0 :(得分:59)

$scope仅适用于控制器和指令的链接功能。这就是过滤器无法找到它的原因。也许你的意思是$rootScope

答案 1 :(得分:1)

我发现“this”引用了本地$ scope(在filter函数内部)。不确定这是否是访问它的安全方式。