I am trying to understand this behaviour, and have added simplified code of what I'm trying to do. In an HTML partial for a modal using a modal controller I have some code like this:
<form>
<!-- example one -->
<div read-field="{{ singleContact._id }}" ></div>
<h2>{{ singeContact._id }}</h2>
<!-- example two -->
<div read-field="{{ someScope }}"></div>
</form>
and the directive:
angular.module('myApp')
.directive('readField', function () {
return {
link: function (scope, element, attrs) {
console.log(attrs); // outputs correct object with attrs.readField "523c2..mongo_id.."
console.log(attrs.readField); // outputs and empty string
}
};
});
Even though the singleContact._id value is rendered correctly on the page in the h2, and appears correctly in the object returned by logging just the (attrs), logging the variable attrs.readField directly returns an empty string. Doing the same thing in example two with someScope and everything works fine, ie a console.log of attrs.someScope returns 'Foo' as it should.
Obviously then it is a problem with how they are setup in the controller: with example one (singleContact) coming from a function call to an api service, and example two (someScope) being a straight declaration.
angular.module('myApp')
.controller('EditModalCtrl', function ($scope, $modalInstance, $rootScope, apiCalls, id) {
apiCalls.getContact(id)
.success(function (data) {
console.log(data);
$scope.singleContact = data;
});
$scope.someScope = 'Foo';
});
Any help in making me understand this would be much appreciated, I am new to angular, and have the feeling I'm missing something important here, thanks.