设置div背景图像仅适用于页面加载

时间:2016-07-21 16:58:40

标签: javascript angularjs

我设置了一个带有setPic()功能的div背景。我使用changePic()函数来更新图片

HTML

div.profileimg(id="img", ng-style="imagePath")

角/ JS

function setPic() {
    console.log("in set pic")
    var url = '/api/image?spotId=' + $window.sessionStorage.getItem("currentGarage")
    $http.get(url).then(
        function successCallback(response) {
            console.log("Success get image")
            $scope.imagePath = {};
            $scope.imagePath = {
                background: 'url(' + url + ')'
            };
        },
        function errorCallback(response) {
            console.log("fail get image")
        });

}


changePic = function(fd) {
    url = "/api/spot/pic/post?spotId=" + $window.sessionStorage.getItem("currentGarage");
    $http.post(url, fd, {
        headers: {
            'Content-Type': undefined
        },
        transformRequest: angular.identity
    }).then(
        function successCallback(response) {
            console.log("Success post image")
            setPic();
        },
        function errorCallback(response) {
            console.log("fail post image")
        });
};

setPic()函数在控制器(工作)的开头调用一次,然后在changePic()函数的success子句中调用一次。为什么在页面加载后调用setPic()不起作用?

还有一个问题。请注意我如何以不同方式定义函数。一个是function name(),另一个是name = function()。从控制器而不是dom调用setPic()。如果我更改此功能的形式,我的网站不会构建,我会在我的字段中看到{{}}changePic()已在按钮的onclick中注册,如果我更改该功能的形式,则不会注册该点击。这里真的很令人兴奋的疯狂。在尝试编码之前我应该​​去javascript / angular课程吗?

0 个答案:

没有答案