UPDATE: this is a duplicate of Call Angular JS from legacy code
I am implementing a new feature in an existing application using angular. I can't rewrite the entire app now so this means models in angular may need to be updated from the rest of the app, which manipulates the dom directly.
How can I update a model without deviating too much (or at all) from the design patterns inherent in the angular?
Here is a simplified working example, but it contains event handlers in the controller. Perhaps there is a better way.
NOTE: the button is imaginary. there is no such button in the application. Its for simplification.
http://plnkr.co/edit/XoD6hPKs2ou3oqpuVhnQ?p=preview
JS:
var testApp = angular.module('testApp', []);
testApp.controller('audioController', function ($scope) {
$scope.settings = {audiostop: "00:30"};
$('button').click(function(){
$scope.loadSettings({audiostop: '23'});
});
$scope.loadSettings = function (data) {
$scope.$apply(function(){
$scope.settings = { audiostop: data.audiostop};
});
}
});
Another option is to declare function for an external object inside of the controller:
NAMESPACE.someobject.loadSettings = function (data) {
$scope.$apply(function(){
$scope.settings = { audiostop: data.audiostop};
});
}
UPDATE: it seems that what I need is to implement dependency injection as explained here : http://docs.angularjs.org/guide/di