Pradeep Pradeep - 3 months ago 18
AngularJS Question

Access $scope variable of ng-include in parent $scope

I have created an angular application where my views are loading in ng-view tag based on the route configuration. In once of my view I am loading sub templates using ng-include based on a dropdown selection dynamically. On button click action on the main view I want to get the scope of the view which is loaded in ng-include. Can anybody suggest me how to do it ?

enter image description here

Answer

You could do something like this to access a particular controller scope.

var ctrlElement = document.querySelector('div');
var ctrlScope = angular.element(ctrlElement).scope();
console.log(ctrlScope.user);

Below is a simple fiddle showing it.

https://jsfiddle.net/2Lj26ru5/