我正在尝试使用角度ng样式将背景图像应用于span标记,当单击span标记时,它会调用chgImg但它似乎不起作用。如果我用/images/chk.svg
直接替换{{img}},它的工作原理 <span ng-style="{'background-image':'url({{img}})'}" ng-click="chgImg()" class="Pollchoice--radio">{{img}}
</span>
AngularCode:
app.controller('HomeCtrl', ['$scope', 'posts',
function($scope, posts) {
$scope.posts = posts.posts;
$scope.chgImg = function(){
$scope.img = "/images/chk.svg";
};
}
]);
答案 0 :(得分:0)
请使用 $ scope.apply ,如下所示:
$scope.chgImg = function(){
$scope.$apply(function () {
$scope.img = "/images/chk.svg";
});
};