One option that, as far as I can tell, provides almost exactly these properties would be to take the difference of the scope that you retrieved (via angular.element(/*...*/).scope()
), and that scope's prototype.
Here's a sample function that does that:
function getAssignedScopeProperties(targetScope){
var targetProto = Object.getPrototypeOf(targetScope);
var assignedProperties = {};
for (var prop in targetScope) {
if (targetScope.hasOwnProperty(prop) && typeof targetProto[prop] === "undefined") {
assignedProperties[prop] = targetScope[prop];
}
}
return assignedProperties;
}
And then using the function:
var targetElement = angular.element(document.querySelector('<selector-name>'));
var targetProps = getAssignedScopeProperties(targetElement.scope());
Unfortunately, in Angular 1.3.15 this seems to leave the $$watchersCount
property. This does not happen in versions 1.3.14, nor 1.3.16, so it was likely a bug in AngularJS for version 1.3.15.
That said, keeping a guard against $$watchersCount
(or a blacklist with it) to defend against versions of Angular with such bugs doesn't feel proper to me. Another option to ensure that this doesn't happen is to include a check for prop.charAt(0) !== "$"
in the inner-if, but assuming the objective is to keep all values that are assigned in the controller, removing any that the controller defined starting with a $
would certainly be wrong (of course, the person who built the controller assigning to properties starting with $
is wrong, too, but that's neither here nor there).