首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >将jquery代码转换为angular代码

将jquery代码转换为angular代码
EN

Stack Overflow用户
提问于 2016-08-10 20:23:00
回答 4查看 2.1K关注 0票数 0

我正在建立一个使用离子角度的天气应用程序,我的应用程序几乎完成,但我想添加一些附加组件。这段jQuery代码我想把它转换成angular:

代码语言:javascript
复制
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");

我试着为angular这样做,但它不起作用:

代码语言:javascript
复制
    $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:

代码语言:javascript
复制
{{tempco(weather.currently.temperature)| number:1}} &deg;
EN

回答 4

Stack Overflow用户

发布于 2016-08-10 20:38:49

您需要编写指令来处理dom操作。

angularjs

代码语言:javascript
复制
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

代码语言:javascript
复制
<span temp-celsius="12"> //use @
<span temp-celsius="info.tempCelsius"> //use =, value in scope
票数 1
EN

Stack Overflow用户

发布于 2016-08-10 20:34:52

如果没有显示任何值,您的函数将不返回任何内容。在末尾添加一条return语句。至于css样式,angular有一个对你有用的ngStyle指令。

票数 0
EN

Stack Overflow用户

发布于 2016-08-10 20:35:48

试着这样做:

代码语言:javascript
复制
$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";
        }       
    }

和html:

代码语言:javascript
复制
  <div style="background-color:{{tempco(weather.currently.temperature)}}">
  {{weather.currently.temperature}}
  </div>

fiddle

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/38873335

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档