角度背景图像改变

时间:2016-08-18 23:44:55

标签: javascript angularjs

我正在使用JS / HTML / CSS / Angular

制作天气应用程序

我正在尝试开发它,以便在天气设置为某种东西时,它会改变背景图像以匹配。

编辑:

以下是我在HTML和JS中使用的两个代码

在我的html文件中:

<body ng-app="Weather" ng-style="{'background-image':'url({{newBg}})'}">

在我的JS中:

  if($scope.Data.des == 'Clear'){
    $scope.newBg = 'http://i.stack.imgur.com/mfvI9.jpg';
  }

  if($scope.Data.des == 'Cloudy'){
    $scope.newBg = 'http://i.stack.imgur.com/mfvI9.jpg';
  }

图片随机只是为了测试。似乎对我不起作用。

1 个答案:

答案 0 :(得分:0)

待办事项 <body ng-app="Weather" ng-style="{{getStyle()}}">

并在控制器中,执行

    $scope.getStyle = function(){
        var url;
        if($scope.Data.des == 'Clear'){
            url = 'http://i.stack.imgur.com/mfvI9.jpg';
        }else if($scope.Data.des == 'Cloudy'){
            url = 'http://i.stack.imgur.com/mfvI9.jpg';
        }
        return {"background-image":"url("+url+")"}


     }