【发布时间】: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("https://image.tmdb.org/t/p/w1280/nbIrDhOtUpdD9HKDBRy02a8VhpV.jpg");">
因此它为元素创建了一个 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("https://image.tmdb.org/t/p/w1280/nbIrDhOtUpdD9HKDBRy02a8VhpV.jpg");">
知道为什么ng-style 会更新,而另一个style 不会更新吗?
【问题讨论】: