I am loading views and scripts on client side. I am trying to achieve to load and call angularjs controller inside my javascript.
- Load container view(v1.html), main script (eg. myscript.js) and angular.js
- myscript.js will load view(v2.html) inside v1.html and angular controller(controller.js)
- v2.html is as:
<div id="v1_id" ng-app="loadApp" ng-controller="loadCtrl"> {{msg}} </div>. - controller.js is as:
angular.module('loadApp', []).controller('loadCtrl', function($scope) {
$scope.load_msg = function(_msg) {
$scope.msg = _msg;
}
});
- Call to controller within myscript.js :
angular.element(document.getElementById('v1_id')).scope().load_msg('Hi')
And error I get is Uncaught TypeError: Cannot read property 'load_msg' of undefined.
Any thought on this, what I am missing here?
Many thanks.
element.scope(). Anyway, try to put your<div>inside the controller, and not everything at the same level, maybe that's it.element.scopeis something you use only for a quick debug session in order to inspect the scope. Using it for something else is a big code-smell.