0

次のような rootScope 変数を作成しました

$rootScope.globalData = data;
$rootScope.globalData.chillerConditions.HeatSource.Value = "ST";    //Default Value
$scope.chillerConditions.HeatSource.Value = 1;                      //Default Value

dataAPIからの戻り値はどこにありますか。また、アイテムのリストを含むオブジェクトであるスコープ変数を作成します。

$scope.chillerAttributes = data.ObjCandidateListChillerAttributes;
$scope.chillerConditions = data.ObjCandidateListConditions;

HTMLには次のものがあります:

<select ng-model="chillerConditions.HeatSource.Value" style="width:53%;" ng-options="item.Id as item.Description for item in ValidRatingHeatSource" ng-change="heatSourceChanged()" id="ddRatingHeatSource" class="form-control search-select designComboboxHeight" data-container="body"></select>

ここValidRatingHeatSource

$scope.ValidRatingHeatSource = \*list of items*\

ドロップダウンの変更時に、関数を作成しました。その中で

if($scope.chillerConditions.HeatSource.Value == 2)
{
  $rootScope.globalData.chillerConditions.HeatSource.Value = "HW";
}
else
{
  $rootScope.globalData.chillerConditions.HeatSource.Value = "ST";
}

今までは私の現在のコードでした。問題は次のとおりです。

上記の関数が呼び出されると、現在の$rootScope変数 ie$rootScope.globalData.chillerConditions.HeatSource.Valueが に変更されるか、"HW"または"ST"にも変更$scope.chillerConditions.HeatSource.Valueされ"HW"ます"ST"

なんでそうなの?

angularjsに組み込みの機能はありますか? 私が間違いを犯している場合は提案してください。新しい提案も大歓迎です。

4

2 に答える 2

2

この動作は JavaScript の動作であり、AngularJS とは何の関係もありません。JavaScript は、オブジェクトが値ではなく参照によってアドレス指定されるオブジェクト指向 (プロトタイプベース) 言語です。たとえば、 car2 を car1 に割り当てると、両方が同じオブジェクト ( JSFiddle )を参照します。

var car1 = {make: "Audi"}
var car2 = car1;
car2.make = "Toyota";

あなたの場合、$rootScope.globalData.chillerConditions.HeatSource$scope.chillerConditions.HeatSourceは同じオブジェクトです。

むしろ、コピーを作成したいようです。angular.Copyでこれを行うことができます

$scope.chillerAttributes = angular.copy(data.ObjCandidateListChillerAttributes);
$scope.chillerConditions = angular.copy(data.ObjCandidateListConditions);
于 2014-12-29T12:56:21.240 に答える