I am creating a simple FHIR client to edit patient data using AngularJS. I can't figure out why the values in the inputs are not bound to values in the object.
Naturally, I could cheat and implement keyup
and $scope.$apply()
, but something tells me this is not how it is supposed to be done in AngularJS.
I've tried using $index
to bind the values directly to the values in the array, but that didn't seem to do anything.
Here is the code:
var app = angular.module('FhirClient', []);
app.controller('PatientCtrl', function ($scope, $http) {
console.log("Evaluating Patient");
var responsePromise = $http.get("http://nprogram.azurewebsites.net/Patient/1?_format=json&_sm_au_=iMH046nNq52RDM6q");
responsePromise.success(function (data, status, headers, config) {
console.log("Successful connection");
$scope.patient = data;
});
responsePromise.error(function (data, status, headers, config) {
alert("AJAX failed!");
});
$scope.$watch('patient', function (newValue, oldValue) {
console.log(newValue);
});
});
And the following HTML:
<div ng-controller="PatientCtrl">
<div class="container">
<h1>Patient Details</h1>
<code>{{patient | json}}</code>
<div class="col-md-6">
<h2>Names</h2>
<div class="list-group" ng-repeat="names in patient.name track by $namesIndex">
<h3><span class="label label-primary">{{x.use}}</span></h3>
<div class="form-group" ng-repeat="lastName in names.family track by $familyNamesIndex">
<label for="familyName">Family Names:</label>
<input class="form-control" ng-model="lastName" />
</div>
<div class="form-group" ng-repeat="firstName in names.given">
<label for="givenName">Given Names:</label>
<input class="form-control" type="text" ng-model="firstName"/>
</div>
</div>
</div>
<div class="col-md-6"></div>
</div>
</div>
and the JSON:
{
"resourceType":"Patient",
"text":
{
"status": "generated",
"div": "<div xmlns='http://www.w3.org/1999/xhtml'><p>Harley N Hobbs</p><p>16 Pier Road</p><p>STANWARDINE IN THE FIELDS</p><p>SY4 7IW</p><p>Date of birth: 1966-06-07</p></div>"
},
"identifier":
[{
"use": "official",
"label": "SSN",
"system": "http://hl7.org/fhir/sid/us-ssn",
"value": "1"
}],
"name":
[{
"use": "official",
"family":[ "Hobbs"],
"given":[ "Harley"]
}],
"telecom":
[{
"system": "phone",
"value": "077 8169 8899",
"use": "home"
}],
"gender":
{
"coding":
[{
"system": "http://hl7.org/fhir/sid/v2-0001",
"code": "male"
}]
},
"birthDate": "1966-06-07",
"deceasedBoolean": false,
"address":
[{
"use": "home",
"text": "16 Pier Road, STANWARDINE IN THE FIELDS, SY4 7IW",
"line":[ "16 Pier Road"],
"city": "STANWARDINE IN THE FIELDS",
"zip": "SY4 7IW"
}],
"careProvider":
[{
"reference": "../organization/@1"
}],
"active": true
}
Any thoughts on what I am doing wrong here?
Here is a simplified JSFiddle: https://jsfiddle.net/gmpp2fyk/1/ . If you edit the textbox, JSON doesn't seem to be updated: $watch
doesn't fire and the expression remains just the way it was.