Friday, 15 August 2014

angularjs - Bind service method to my frontend - Angular -



angularjs - Bind service method to my frontend - Angular -

i have service in angular has 1 method in there - getname() returns someones name

i want in html able {{ $scope.getname() }} , have bound method result. main issue if name in service changes (through controller) {{ ..... }} change.

hopefully makes sense.

regards, yannis

you can't bind dynamic service calls dom element.

problably best way $watch name changes within service, , utilize callback function on change. you'd need way notify controller name has changed, event listener should set up. listener phone call function update scope variable, $scope.namefromservice. , in html you'd need within controller part:

{{namefromservice}}

again, create clear, happens:

another controller changes value in service $watch gets triggered , calls function (among other things) triggers event the event listener in controller receives event , updates scope variable angular automatically updates dom

update

if can alter service, i'd avoid whole $watch thing , modify name setter function triggers event when called. way, controllers can aware of name changes.

angularjs

No comments:

Post a Comment