Background: I am in search of a pure Angular JS solution to address this issue, utilizing the framework’s functionality rather than creating workarounds that involve searching for ng-class
values within the DOM.
The template structure is as follows:
<div class="stuff" ng-class="{ 0: 'default', 1: 'thing_1', 2: 'thing_2', 3: 'thing_3', 4: 'thing_4', 5: 'thing_5' }[thingType]">
In my controller, I can assign a value like so, which will add thing_2
to the stuff
class of the DIV:
$scope.thingType = 2;
However, I am interested in accessing the values set in the ng-class
template directly within the controller. While I understand this data is parsed as JSON internally by Angular, it would be beneficial if the controller could autonomously count the values defined in the template.
My objective is to extract the ng-class
JSON object into the controller. Ideally, even if I were to introduce a new class option indexed from 0
to 5
within the CSS JSON in the template, the controller should automatically count these keys without manual adjustments in both the template and controller.
Despite differing opinions on this strategy, I am seeking a straightforward and native Angular solution to achieve this. Are there any clean methods to accomplish this task? Can this be achieved solely using Angular's logic, avoiding the need for external DOM searches with directives or other makeshift solutions? My focus is on implementing the cleanest and most efficient Angular-native approach available.