我創建JavaScript對象的新副本並將其分配到$scope
有一個奇怪的問題。該對象綁定到前端窗體,所以我必須重置此對象的數據。下面是代碼: 這是JavaScript類:無法刪除Javascript對象,並在角度js範圍內創建新實例
var userInfo = (function() {
function userInfo() {}
var _refCode = "";
var _country = {};
var _language = {};
var _userType = 0;
var _retailer = {};
userInfo.prototype.data = {
refCode: function (value) {
return angular.isDefined(value)
? (_refCode = value)
: _refCode;
},
country: function (value) {
return angular.isDefined(value)
? (_country = value)
: _country;
},
language: function (value) {
return angular.isDefined(value)
? (_language = value)
: _language;
},
userType: function (value) {
return angular.isDefined(value)
? (_userType = value)
: _userType;
},
retailer: function (value) {
return angular.isDefined(value)
? (_retailer = value)
: _retailer;
}
}
return userInfo;
})();
這是我的角度控制器:
app.controller('MainController', function ($scope, $http, $state) {
$scope.initUserInfo = function() {
//here make instance of the class
var user_info = new userInfo();
delete $scope.userInfo;
$scope.userInfo = user_info;
};
$scope.initUserInfo();
});
但是當我調用該函數initUserInfo()
沒有任何反應。 $scope.userInfo
中的數據仍然存在。我做了什麼錯誤以及如何從userInfo
刪除信息?
UPDATE:
謝謝,我做出改變,現在我的代碼的樣子,但是當我
console.log
我有以下錯誤:
Cannot read property 'data' of undefined - on the first console.log
print old value - on second console.log
print old value - on third console.log
Аfter的執行她表演前的這個功能仍然沒有被抹去。
$scope.initUserInfo = function() {
delete $scope.userInfo;
var user_info = null;
console.log("after delete: " + $scope.userInfo.data.refCode());//first console log
user_info = new userInfo();
$scope.userInfo = user_info;
console.log("after assing: " + $scope.userInfo.data.refCode());
$scope.userInfo.data.createdStore(new addStoreData());
$scope.userInfo.merge(verifiedEmployee);
$scope.verifiedEmployee = verifiedEmployee;
console.log($scope.userInfo.data.refCode());
};
爲什麼不能像$ scope.userInfo = {}? –
@ManuelObregozo並嘗試過,但沒有成功 –