正在自动更新范围分配,而不调用angularjs
我有两个作用域函数,当我点击按钮时,它们中的任何一个才会被调用,但一旦被调用,我意识到作用域变量的值每次都会自动更新正在自动更新范围分配,而不调用angularjs,angularjs,angularjs-directive,angularjs-scope,Angularjs,Angularjs Directive,Angularjs Scope,我有两个作用域函数,当我点击按钮时,它们中的任何一个才会被调用,但一旦被调用,我意识到作用域变量的值每次都会自动更新 $scope.images = []; $scope.imagesAttached =[]; $scope.takePhoto = function(index) { if(modalExists === true) { $scope.modal1.hide(); } $scope.showSendButton = true;
$scope.images = [];
$scope.imagesAttached =[];
$scope.takePhoto = function(index) {
if(modalExists === true) {
$scope.modal1.hide();
}
$scope.showSendButton = true;
$scope.attachedImageExists = false;
if($scope.imagesAttached.length > 0) {
$scope.images = $scope.imagesAttached
$scope.attachedImageExists = true;
}
var options = {
destinationType : Camera.DestinationType.FILE_URI,
sourceType : Camera.PictureSourceType.CAMERA,
allowEdit : false,
encodingType: Camera.EncodingType.JPEG,
popoverOptions: CameraPopoverOptions,
correctOrientation: true
};
// 3
$cordovaCamera.getPicture(options).then(function(imageData) {
// 4
var imagetype;
onImageSuccess(imageData);
function onImageSuccess(fileURI) {
createFileEntry(fileURI);
}
function createFileEntry(fileURI) {
window.resolveLocalFileSystemURL(fileURI, copyFile, fail);
}
// 5
function copyFile(fileEntry) {
var name = fileEntry.fullPath.substr(fileEntry.fullPath.lastIndexOf('/') + 1);
var newName = (new Date()).getTime() + name;
window.resolveLocalFileSystemURL(cordova.file.dataDirectory, function(fileSystem2) {
fileEntry.copyTo(
fileSystem2,
newName,
onCopySuccess,
fail
);
}, fail);
}
// 6
function onCopySuccess(entry) {
$scope.$apply(function() {
$scope.modal.remove();
$scope.activeSlide = index;
if(modalExists === false) {
$ionicModal.fromTemplateUrl('image-modal.html', {
scope: $scope,
}).then(function(modal) {
$scope.modal1 = modal;
$scope.modal1.show();
modalExists = true;
$scope.images.push({file: entry.nativeURL, type: $scope.imagelist});
console.log($scope.imagesAttached.length)
console.log($scope.images.length)
});
}
else {
$scope.modal1.show();
$scope.images.push({file: entry.nativeURL, type: $scope.imagelist});
console.log($scope.imagesAttached.length)
console.log($scope.images.length)
}
});
}
function fail(error) {
console.log("fail: " + error.code);
}
}, function(err) {
console.log(err);
});
}
$scope.sendPhoto = function() {
$scope.imagesAttached = angular.copy($scope.images);
}
my image-modal.html页面
<script id="image-modal.html" type="text/ng-template">
<div class="modal image-modal transparent">
<ion-header-bar class="bar bar-header bar-dark">
<div class ="row">
<div class ="col">
<label ng-click="closeModal(1)">Cancel</label>
</div>
<div class ="col">
<label ng-click="deleteImage()">Delete</label>
</div>
<div class ="col" id="send-images" ng-show="showSendButton">
<label ng-click="sendtoAttach()">Send</label>
</div>
</div>
</ion-header-bar>
<ion-slide-box on-slide-changed="slideChanged($index)" show-pager="true" active-slide="activeSlide" >
<ion-slide ng-repeat="image in images">
<img ng-src="{{image.file}}" class="fullscreen-image"/>
</ion-slide>
</ion-slide-box>
<div class="row">
<ion-scroll>
<img ng-repeat="image in images" ng-src="{{urlForImage(image.file)}}" class="image-list-thumb" height="50px"/>
</ion-scroll>
<button class="ion-camera" ng-click="takePhoto()"></button>
</div>
</div>
</script>
我有两个按钮,拿和发
当我第一次调用takePhoto和SendPhoto时,值是正确的,会推送一个图像,并且我的$scope.images和$scope.imagesAttached的长度是1
但如果我再次单击takePhoto按钮,而不调用SendPhoto按钮,我的$scope.images和$scope.imagesAttached长度都将更新为2,而它应该仅为$scope.images=2,而$scope.imagesAttached=1,因为我还没有调用$scope.SendPhoto
我知道angularJS有一些带有$apply和$digest的双绑定功能,但不确定它是如何工作的,为什么它会自动绑定我的范围变量
这与Angular无关,这纯粹是JavaScript对象引用在起作用 将$scope.images指定给$scope.imagesAttached时,两个变量都引用同一个对象 请在sendPhoto函数中尝试此选项
这与Angular无关,它纯粹是工作中的JavaScript对象引用 将$scope.images指定给$scope.imagesAttached时,两个变量都引用同一个对象 请在sendPhoto函数中尝试此选项
菲尔的回答很好。菲尔的回答很好。@KingsleySimon您必须更新您的问题代码。我看不出这怎么可能仍然是最重要的case@KingsleySimon然后你必须更新你的问题代码。我不明白怎么会这样
$scope.imagesAttached = angular.copy($scope.images)