2015-07-03 30 views
0

更換整個對象我想要更新一個JSON回調的objectValue
根據以下代碼,如果我離開objectValue.data它只更新對中的值。我試過把.data關掉,但沒有迴應。AngularJS/JavaScript的 - 我如何使用JSON

有沒有一種方法,我可以用JSON請求我得到完全覆蓋objectValue

app.service('sharedProperties', function() { 
    var objectValue = { 
     'data': 'Click to change' 
    }; 
    return { 
     setObject: function(value) { 
      objectValue.data = value; 
     }, 
     getObject: function() { 
      return objectValue; 
     } 
    }; 
}); 

乾杯!

+0

你是什麼意思'它只更新對中的值'?你想要的結果是什麼? – hogan

+0

我的意思是,當它更新它變成'數據':{「json」:{「bit」:[{「keyname」:「key」,「valuename」:value},{「keyname」:「key」 VALUENAME「:值},{」鍵名「:」關鍵」,‘值名’:值} ...等 ID喜歡它放棄什麼現成的,只留下了我的JSON作爲對象 – bronze

+0

你有一個jsfiddle? – hogan

回答

1

我看不出這是一個問題。我試過了:

....  
// service 
    setObject: function(value) { 
     objectValue = value; 
    }, 


// and in the controller 
app.controller('myController', function($scope, sharedProperties) { 
    $scope.stringValue = sharedProperties.getString; 
    $scope.objectValue = sharedProperties.getObject(); 
    $scope.setString = function(newValue) { 
     $scope.objectValue.data = newValue; 
     sharedProperties.setObject(newValue); 
     alert(sharedProperties.getObject()); 
    }; 
}); 

並且它警告了這個對象。也許你的代碼的另一部分有問題。嘗試逐步記錄/警報,看看會發生什麼。