0

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

.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;
  }
}

如您所见,我将 my 设置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没有?

4

1 回答 1

1

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

.movie-background{"ng-style" => 
      "{'background-image':'url(https://image.tmdb.org/t/p/w1280' + movie.backdrop +')'}"}
于 2016-01-16T18:33:07.120 回答