I think you should take a look at ngDoCheck and OnChanges lifecycle bindings in Angular2 and $ apply in your Angular1.
using $ apply your controller is changed as below
var myApp = angular.module("hybridExampleApp", []); //define as Angular 1 service myApp.service('UserService', UserService); myApp.controller('MainController', function ($scope, UserService) { $scope.$watch('username',function(){ $scope.setUser(); }); $scope.getUsers = function () { UserService.setUser($scope.username); window.location = './angular2.html' }; $scope.setUser = function () { UserService.setUser($scope.username); $scope.user = $scope.username; }; $scope.getUser = function () { $scope.user = UserService.getUser(); }; });
Using ngDo Check your user.section.component.ts as below
import {Component, Inject,ChangeDetectionStrategy,ngDoCheck,ngOnChanges} from 'angular2/core'; @Component({ selector: 'user-section', templateUrl: './src/ng2-component/user.section.tpl.html', changeDetection: ChangeDetectionStrategy.OnPush }) export class UserSection implements ngDoCheck, ngOnChanges{ username123:number; constructor(@Inject('UserService') userService:UserService) { this.users = []; this.username = ''; this.user=0; this._UserService = userService; } ngOnChanges(){ console.log('adfkjna'); } getUsers():void{ this.users = this._UserService.getUsers(); } setUser():void{ this._UserService.setUser(this.username); } getUser():void{ this.user = this._UserService.getUser(); } ngDoCheck(){ this.user = this._UserService.getUser(); this.getUser(); console.log(this.user); } }
My code is fully logged , the username is changed to angular 1, but I cannot understand why it does not bind it to the user interface .
LIVE DEMO of your updated plunger.
source share