Generally it is considered bad to touch the DOM from a controller.
However, in your case I don't see other options and as you're only reading from the DOM and not manipulating it in any way it's not as bad.
Assuming you have the text rendered in a controller, you can use $element
to gain access to the element and then fetch the text content. Again, this is a last resort and against the Angular philosophy.
var app = angular.module("myApp", [])
app.controller("HomeController", HomeController);
function HomeController($scope,$element) {
$scope.vars = {};
$scope.vars.color = "Black";
$scope.theme = {color:{}};
$scope.theme.color.black = "#000000";
$scope.snap = function(){
alert($element.find("h3").text()); // will alert the text
}
}
Alternatively, you can decouple this logic from a controller (and the used DOM) completely using $compile
. This is a rather long example but it gives you a peek into Angular's way of doing things, this doesn't require an app.
var $inj = angular.injector(["ng"]); // get access to Angular's services
var template = "<h3>{{vars.color}} is my color: {{theme.color.black}}</h3>"
//now let's access the compilation service, and also a scope service and get a new scope.
var $compile = $inj.get("$compile");
var $scope = $inj.get("$rootScope").$new(true);
// put our stuff in this new scope
$scope.vars = {color:"White"};
$scope.theme = {color:{black:"#000000"}};
// add the template
var ngTemplate = $compile(angular.element(template));
var result = ngTemplate($scope);
$scope.$apply(); // tell angular to apply changes to the scope
console.log(result.text()); // now we have our rendered text.
// calling $destroy on the scope here is probably good ;)
However, are you sure what you're looking for is not partial views? What is the purpose of this?