0

I am building a weather app using ionic angular , my app is almost done , but I want add some add-on. This jQuery code I want to it to be converted to angular:

var tempCelsius = Math.round(data.current_observation.temp_c);
        if (tempCelsius < 15){
            $("#temp").css("color", "#00DFF9");
        } else if (tempCelsius < 20){
            $("#temp").css("color", "#21DBE1");
        } else if (tempCelsius < 25) {
            $("#temp").css("color", "#A0FF74");
        } else if (tempCelsius < 30) {
            $("#temp").css("color", "##FEB900");
        } else if (tempCelsius < 35) {
            $("#temp").css("color", "##FE7400");
        } else if (tempCelsius < 40) {
            $("#temp").css("color", "#FE5100");
        } else if (tempCelsius > 45) {
            $("#temp").css("color", "#FE0000");
        } else if (tempCelsius > 50) {
            $("#temp").css("color", "##FE0000");
        } else if (tempCelsius > 55) {
            $("#temp").css("color", "#E8250C");
        }


        $("#temp").html(Math.round(tempCelsius )+ "&#176;C");

I try to make it like that for angular but it is not working :

    $scope.tempco =function(tempco){    
    var tempCelsius = Math.round(weather.currently.temperature);
        if (tempCelsius < 15){
            $("#temp").css("color", "#00DFF9");
        } else if (tempCelsius < 20){
            $("#temp").css("color", "#21DBE1");
        } else if (tempCelsius < 25) {
            $("#temp").css("color", "#A0FF74");
        } else if (tempCelsius < 30) {
            $("#temp").css("color", "##FEB900");
        } else if (tempCelsius < 35) {
            $("#temp").css("color", "##FE7400");
        } else if (tempCelsius < 40) {
            $("#temp").css("color", "#FE5100");
        } else if (tempCelsius > 45) {
            $("#temp").css("color", "#FE0000");
        } else if (tempCelsius > 50) {
            $("#temp").css("color", "##FE0000");
        } else if (tempCelsius > 55) {
            $("#temp").css("color", "#E8250C");
        }       
    }

html :

{{tempco(weather.currently.temperature)| number:1}} &deg;
4
  • you need to return a value from your tempco function. currently you return nothing so tempco(weather.currently.temperature) will be undefined Commented Aug 10, 2016 at 12:30
  • Did you get any error ? Commented Aug 10, 2016 at 12:33
  • what the solution ? Commented Aug 10, 2016 at 12:33
  • @DMCISSOKHO no error Commented Aug 10, 2016 at 12:34

4 Answers 4

1

you need write a directive to handle dom operation

angularjs

YourApp.directive('tempCelsius', function () {
    return {
        scope: {
            tempCelsius:'=', //or @
        }
        restrict: 'A',
        link: function (scope, element) {
            var $el = $(element); //need jquery js. (don't recommend use jquery & angularjs)
            var tempCelsius = scope.tempCelsius;
            if (tempCelsius < 15){
                $el.css("color", "#00DFF9");
            } else if (tempCelsius < 20){
                $el.css("color", "#21DBE1");
            } else if (tempCelsius < 25) {
                $el.css("color", "#A0FF74");
            } else if (tempCelsius < 30) {
                $el.css("color", "##FEB900");
            } else if (tempCelsius < 35) {
                $el.css("color", "##FE7400");
            } else if (tempCelsius < 40) {
                $el.css("color", "#FE5100");
            } else if (tempCelsius > 45) {
                $el.css("color", "#FE0000");
            } else if (tempCelsius > 50) {
                $el.css("color", "##FE0000");
            } else if (tempCelsius > 55) {
                $el.css("color", "#E8250C");
            }   
        }
    }
});

html

<span temp-celsius="12"> //use @
<span temp-celsius="info.tempCelsius"> //use =, value in scope
Sign up to request clarification or add additional context in comments.

Comments

0

For no value being displayed, your function returns nothing. Add a return statement at the end. As for the css styling, angular has an ngStyle directive that will be useful to you.

Comments

0

Try like this:

$scope.tempco =function(tempco){    
    var tempCelsius = Math.round(weather.currently.temperature);
        if (tempCelsius < 15){
            return "#00DFF9";
        } else if (tempCelsius < 20){
            return "#21DBE1";
        } else if (tempCelsius < 25) {
            return "#A0FF74";
        } else if (tempCelsius < 30) {
            return "##FEB900";
        } else if (tempCelsius < 35) {
            return "##FE7400";
        } else if (tempCelsius < 40) {
            return "#FE5100";
        } else if (tempCelsius > 45) {
            return "#FE0000";
        } else if (tempCelsius > 50) {
            return "##FE0000";
        } else if (tempCelsius > 55) {
            return "#E8250C";
        }       
    }

and html:

  <div style="background-color:{{tempco(weather.currently.temperature)}}">
  {{weather.currently.temperature}}
  </div>

fiddle

Comments

0

try this

 $scope.tempco =function(tempco){ 
 $scope.colorCode = "";   
var tempCelsius = Math.round(weather.currently.temperature);
    if (tempCelsius < 15){
        $scope.colorCode = "#00DFF9";
    } else if (tempCelsius < 20){
        $scope.colorCode = "#21DBE1";
    } else if (tempCelsius < 25) {
        $scope.colorCode = "#A0FF74";
    } else if (tempCelsius < 30) {
        $scope.colorCode = "#FEB900";
    } else if (tempCelsius < 35) {
        $scope.colorCode = "#FE7400";
    } else if (tempCelsius < 40) {
        $scope.colorCode = "#FE5100";
    } else if (tempCelsius > 45) {
        $scope.colorCode = "#FE0000";
    } else if (tempCelsius > 50) {
        $scope.colorCode = "#FE0000";
    } else if (tempCelsius > 55) {
        $scope.colorCode = "#E8250C";
    }       
}

$scope.tempco(parameter); //pass any value as parameter while calling

html

<div ng-style="{'color': colorCode}"> 
   {{tempco(weather.currently.temperature)| number:1}}&deg;
</div>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.