angularjs adding functions and properties to a controller
February 5, 2023 7:05 AM   Subscribe

I'm trying to extend or add to an angularjs controller because I have a controller being used in 2 sites with 90% shared functionality but one of the sites is used by staff so they have a little extra functionality. So I saw a tutorial on extending a controller with additional functionality but its not quite working right - when functions in the child controller change properties/models the values change but the view doesn't update

I made a very simple app to test the concept JSFiddle

So clicking the extended function button , the text in the view for cal.extended_property updates, but clicking the base function button, the console output indicates the function is run and that the base_property has been updated but the value in the view doesn't change

I tried added $scope.$apply() but that just throws an error Error: [$rootScope:inprog] http://errors.angularjs.org/1.6.4/$rootScope/inprog?p0=%24apply (not ideal anyway as the real script is a lot more complex and I didn't want to have to be running that every time the base controller changes a property)

If there's a better way to achieve what I'm trying to do that isn't too complicated, I'm open to that too. Essentially I have a controller and I want to, in some circumstances, add additional properties and methods to that controller and ideally have it function seemlessly as if it were all the 1 controller
posted by missmagenta to Computers & Internet (2 answers total)
 
Best answer: I'm not an angular expert, but I think I was able to get it to work by changing vm to this in baseFunction. I think the context changes after the controller is extended and that function is executed in the new context. Here is a JSFiddle.
posted by alphanerd at 10:08 AM on February 5, 2023 [1 favorite]


Response by poster: Thanks so much for this, it got me on the right track - I created a function in the parent controller that does vm=this and then called that from the child and that seems to do the job so I don't need to rewrite all the code. Just hope it works on the real thing!
posted by missmagenta at 1:33 AM on February 6, 2023 [1 favorite]


« Older Desperately seeking food ideas   |   Where would you take a foreign exchange student on... Newer »
This thread is closed to new comments.