Grant
Grant

Reputation: 11386

AngularJS - Access Model Property Without Controller

I have been following some of the introductory AngularJS tutorials and they start off by showing how easy it is to get started.

They say all you have to do is define ng-app and then use the example of an input with ng-model="name" and then show that string as you type using {{name}}.

My question is, is it possible to access and read the 'name' property without having to define a controller and read it using $scope.name?

For example..

$(function () {

    var name = ?.name;

});

Upvotes: 2

Views: 1487

Answers (1)

Chandermani
Chandermani

Reputation: 42669

Well as it turns out, setting ng-model without a ng-controller defined at any parent level creates the item on the $rootScope.

What it also means that whereever $rootScope dependency can be injected you can get access to variable you define in the view.

See my fiddle here

Update: Based on the question update. You can access the scope outside from angular using something like

angular.element(domElement).scope()

and the access the variable. See this answer. But please avoid as much as possible.

Upvotes: 4

Related Questions