视图不会更改背景图像

时间:2016-01-16 18:29:46

标签: css angularjs ng-style

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

.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属性会使用新网址进行更新,但样式属性不会更改。

<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没有更新?

1 个答案:

答案 0 :(得分:1)

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

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