Angular js在两个功能中注重价值

时间:2018-01-11 07:11:22

标签: angularjs

我想根据从后端收到的数据更新我的数据

有两个功能:

function getData(data){
Service.getRequest(url).then(function (_response) {
                console.log(_response)
                var res = _response
                if(res.data.success) {
                    $scope.$watch('data.photo', function() {
                        alert('hey, myVar has changed!');
                    });
                    data.photo = res.data.result;// this will tell that Image received against the corresponding data
                }
                else {
                    ctrl.errorCallBack =  true
                    ctrl.errorFunction =  5
                }
                $rootScope.showPreloader = false;
            });

}

现在我有第二个函数接收所选数据并在点击按钮时调用,现在问题是我可以在上面的Get请求完成时更新此函数内的数据:

function syncData(data) {
   console.log('image received')
   console.log(data)

}

第一个函数基于图像数被调用n次,现在用户点击第二个函数来查看图像但是用户看不到任何图像因为还没有收到图像但是我想要一种更新第二个函数的方法,因为Image是收到了

实例 ist图像已加载....用户未点击第二个功能

第二张图片待定....用户点击第二个功能或第二个功能处于活动状态

收到第2张图像....收到活动功能图像

https://plnkr.co/edit/zKeDT84W6eF9cc3FpcmG?p=preview

1 个答案:

答案 0 :(得分:1)

如果对变量使用$ scope,只要它发生变化,它们就会自动与DOM同步。

我在plnkr为你做了一些改变,希望能解决你的问题。

  

http:// plnkr.co/edit/Ug53a03kB7Ap1EWf2jZK?p=preview