简体   繁体   English

如何使用AngularJS中的相同控制器在视图之间共享变量?

[英]How can i share variables between views, with the same controller in AngularJS?

I have this controller... 我有这个控制器...

app.controller('UploadController', ['$scope', 'Upload', '$timeout', function ($scope, Upload, $timeout) {
    $scope.$watch('files', function () {
        $scope.upload($scope.files);
    });
    $scope.upload = function (files) {
        if (files && files.length) {
            for (var i = 0; i < files.length; i++) {
                var file = files[i];
            }
        }
    };
}]);

i'm assigning to the var file the temp obtained from files[i] , so i can display the temp like a preview on my view. 我正在将从files[i]获得的温度分配给var file ,因此我可以像预览一样在我的视图上显示该温度。

 <section ng-controller="UploadController">
      <img ngf-src="files[0]" ng-show="files[0].type.indexOf('image') > -1">
 </section>

but what I really need is to show the temp in my modal view. 但是我真正需要的是在模态视图中显示温度。

 <section ng-controller="UploadController">
      <img ngf-src="files[0]" ng-show="files[0].type.indexOf('image') > -1">
 </section>

The modal view have the same controller and the same "img" attributes, but when i display the modal view the controller RESTART , this is the point of the question, i need to learn how can i keep the value of a variable in the same controller but in an different view without the need to redo actions. 模态视图具有相同的控制器和相同的“ img”属性,但是当我显示模态视图时,控制器RESTART ,这是问题的重点,我需要学习如何将变量的值保持在相同的位置。控制器,但视图不同,无需重做操作。

basically i need: when displaying a view, my controller does not restart . 基本上我需要:显示视图时,我的控制器不会重新启动

<img src=""> takes image's url or image's path. <img src="">采用图片的网址或图片的路径。 Once you upload the image to the server. 将映像上传到服务器后。 If you could returns the image url instead of the image data, the image should be displayed properly. 如果可以返回图像URL而不是图像数据,则应正确显示图像。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM