我有一个模板,我在其中调用一个函数,我有一个指令元素,
.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("https://image.tmdb.org/t/p/w1280/nbIrDhOtUpdD9HKDBRy02a8VhpV.jpg");">
因此它为元素创建了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("https://image.tmdb.org/t/p/w1280/nbIrDhOtUpdD9HKDBRy02a8VhpV.jpg");">
为什么ng-style
会更新,但另一个style
没有更新?
答案 0 :(得分:1)
在{{}}
内部不需要使用ng-style
,在+
表达式中连接范围变量时使用ng-style
进行字符串连接。
.movie-background{"ng-style" =>
"{'background-image':'url(https://image.tmdb.org/t/p/w1280' + movie.backdrop +')'}"}