angularjs - link 2 javascript obejcts -
i working on angularjs app , found problem cant solve. have variable predefined text want replace in email actual values, looks this:
$scope.predefinedvars = { 'client_name': $scope.itempartner.name, 'client_city': $scope.itempartner.city, 'client_county': $scope.itempartner.county, 'client_address': $scope.itempartner.address, 'client_phone': $scope.itempartner.phone, 'client_email': $scope.itempartner.email };
and on...
now later on, when choose partner, itempartner
object changes data. example: have function watch when change partner select box:
$scope.$watch('newcontract.partner_id', function() { $scope.itempartner = _.where($scope.listpartners, {'id': $scope.newcontract.partner_id})[0]; alert(json.stringify($scope.itempartner)); });
now, in alert, can see itempartner
data has changed, if try read values 1st variable $scope.predefinedvars
, values still empty , not change.
is there way force values change when change itempartner
object ?
the problem have set fields of predefinedvars
object primitive. in javascript, primitives passed value, not reference (google if you're not sure means).
the result reference object used set lost.
you have couple of alternatives:
instead of replacing email using data
$scope.predefinedvars
, use data$scope.itempartner
. less work.create function repopulates
predefinedvars
e.g.
function populatepredefinedvars (partner){ $scope.predefinedvars = { 'client_name': partner.name, 'client_city': partner.city, 'client_county': partner.county, 'client_address': partner.address, 'client_phone': partner.phone, 'client_email': partner.email }; } $scope.$watch('newcontract.partner_id', function() { $scope.itempartner = _.where($scope.listpartners, {'id': $scope.newcontract.partner_id})[0]; populatepredefinedvars($scope.itempartner); });
Comments
Post a Comment