angular 1.6 unable to show scope value in template

615 Views Asked by At

I have index.html

Previous

<body ng-app="MainController">
     <div class="page page-base {{ pageClass }}" ng-view>
     </div>
</div>

I assigned some values to $scope.errorMsg in registerController so changed Changed to

    <body ng-app="myApp">
     <div class="page page-base {{ pageClass }}" ng-view>
        <div ng-controller="MainController" ng-click="doClick($event)"></div>
        <div ng-controller="RegisterController"></div>
     </div>
</div>

In controller when I do console.log It print the value but it is not getting displayed on error.html page.

$scope.errorMsg = data.message;
$location.path("/reg/error");

I am new to Angular, Please let me know if I need to add in more info for my problem

1

There are 1 best solutions below

4
On BEST ANSWER

Try interpolation in the scope of RegisterController:

<div ng-controller="RegisterController">{{errorMsg}}</div>

Update

A common source of confusion to those new the angular is that scopes and controllers come and go with the wind, and it's not obvious where to place transient data.

Since angular services are singletons, one solution is to implement a service to store transient state - in the OPs case, "errorMsg" - among view changes.

Here's a bare bones singleton service.

app.factory('StateService', function() {
   // A singleton object to stuff transient data
   return { };
});

It's easily passed along to controller instances like so

app.controller('ErrorController', ['$scope', 'StateService', function($scope, states) { 
 . . .
});

Here's a plunkr that demonstrates the concept, using your code as a base.