有时,当两个变量引用同一个变量时,它们不会像这样绑定在一起:
var option = 1;
$scope.a = option;
$scope.b = option;
当您更改 $scope.a 时,$scope.b 不会更改。看到这个Plunker
但是,有时它们会绑定在一起,例如它以这样的模态发生在我身上:
angular.module('plunker', ['ui.bootstrap']);
var ModalDemoCtrl = function($scope, $modal, $log) {
$scope.testDataArray = [{
"name": "Doe"
}, {
"name": "Deer"
}, {
"name": "Female"
}];
$scope.open = function(testData) {
var modalInstance = $modal.open({
templateUrl: 'myModalContent.html',
controller: ModalInstanceCtrl,
resolve: {
data: function() {
return testData;
}
}
});
modalInstance.result.then(function(selectedItem) {
$scope.scopeData = selectedItem;
}, function() {
$log.info('Modal dismissed at: ' + new Date());
});
};
};
// Please note that $modalInstance represents a modal window (instance) dependency.
// It is not the same as the $modal service used above.
var ModalInstanceCtrl = function($scope, $modalInstance, data) {
$scope.modalData1 = data;
$scope.modalData2 = data;
$scope.ok = function() {
$modalInstance.close($scope.modalData);
};
$scope.cancel = function() {
$modalInstance.dismiss('cancel');
};
};
见Plunker。这里的“modalData1”和“modalData2”都指的是“数据”。在此 Plunker 的任何模态中,您更改 modalData1,然后 modalData2 随之更改。
为什么会这样??
谢谢!