【问题标题】:view does not change background image视图不改变背景图像
【发布时间】:2016-04-22 04:45:51
【问题描述】:

我有一个模板,我在其中调用一个函数,并且我有一个指令元素,

.container-wrapper{"ng-controller" => "MovieRowCtrl"}
  %i.fa.fa-info-circle{"ng-click" => "updateSelectedMovie(movie)"}

#big-box-container{"ng-if" => "rowActive"}
  %movie-details{:movie => "selectedMovie"}

函数在指令中,

app.directive('movieDetails', MovieDetailsDirectiveFn)
  .controller('MovieRowCtrl', ['$scope', '$rootScope', MovieRowCtrlFn]);

function MovieDetailsDirectiveFn($animate) {
  return {
    restrict: 'E',
    scope: {
      movie: '=',
    },
    templateUrl: '../assets/angular-app/templates/_movie-info.html'
  }
}

function MovieRowCtrlFn($scope, $rootScope) {
  $scope.updateSelectedMovie = function updateVisibleMovieIndexFn(movie) {
    $scope.selectedMovie = movie;
    $rootScope.$broadcast('movieRowActivated', {targetScopeId: $scope.$id});
    $scope.rowActive = true;
  }
}

如您所见,我将MovieRowCtrl 设置为.container-wrapper 的控制器。

因此,当updateSelectedMovie(movie) 函数触发时,它会将$scope.rowActive 设置为true(显示元素),并将_movie-info.html 模板注入%movie-details 元素中。

这很好用。

在那个_movie-info.html 模板中我有这个,

.movie-background{"ng-style" => "{'background-image':'url(https://image.tmdb.org/t/p/w1280{{movie.backdrop}})'}"}

结果,

<div class="movie-background" ng-style="{'background-image':'url(https://image.tmdb.org/t/p/w1280/lXOqBOcx1t5A3YhJEIfJZOkigwH.jpg)'}" 
style="background-image: url(&quot;https://image.tmdb.org/t/p/w1280/nbIrDhOtUpdD9HKDBRy02a8VhpV.jpg&quot;);">

因此它为元素创建了一个 ng-style 和一个 normale 样式属性。

问题是,当我触发 updateSelectedMovie(movie) 时,ng-style 属性会更新为新的 url,但样式属性不会改变。

<div class="movie-background" ng-style="{'background-image':'url(https://image.tmdb.org/t/p/w1280/15PbZtjRJ4zgQA8XS0otL70piQi.jpg)'}" 
style="background-image: url(&quot;https://image.tmdb.org/t/p/w1280/nbIrDhOtUpdD9HKDBRy02a8VhpV.jpg&quot;);">

知道为什么ng-style 会更新,而另一个style 不会更新吗?

【问题讨论】:

    标签: css angularjs ng-style


    【解决方案1】:

    ng-style 内不需要使用{{}},在ng-style 表达式内连接作用域变量时,使用+ 进行字符串连接。

    .movie-background{"ng-style" => 
          "{'background-image':'url(https://image.tmdb.org/t/p/w1280' + movie.backdrop +')'}"}
    

    【讨论】:

    • 啊,我明白了,您删除了movie.backdrop 周围的单引号。噗,完全错过了。谢谢
    • @alucardu 接受并投票回答,如果有帮助的话..谢谢 :)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-10-13
    • 2014-02-17
    • 2019-05-15
    相关资源
    最近更新 更多