how to hide angularJs Code when page is refreshed? - angularjs

how to hide angularJs Code when page is refreshed ?
please see this :
how to hide {{userInfo.UserName}} when page is refreshed ?
this is Not user-friendly.

Just use ng-cloak:
<div id="template1" ng-cloak>{{ 'hello' }}</div>
<div id="template2" class="ng-cloak">{{ 'world' }}</div>
it hides given elements until your application is loaded. Note that documentation states that for best results, you should load angular.js in the head section, what ensures that functionality is accessible right after head has been processed.

Related

AngularJS - Page loads before scope variable is initialised

I am initialising a scope variable to false in controller
$scope.created=false;
However when the page loads for the fist time still the below message flickers once and then hides. Is there a way to stop this flicker and hide it fully when the page loads for the first time.
<div class="alert alert-success" ng-show="created">A new Entity created successfully.</div>
add ng-cloak to your div, it hides it until the controller is loaded.
Jay,
You can try this:
<div ng-init="created=false">
<div class="alert alert-success" ng-show="created">A new Entity created successfully.</div>
</div>
And finally you can create a function that makes the variable created be true.
Look an example: Plunker

multiple ng-view wth ng-if

In my application the index page is like
<div ng-if="isLoggedIn>
<div ng-view>
</div>
<div ng-if="!isLoggedIn">
<div ng-if="type==='admin'">
<div ng-view>
</div>
</div>
<div ng-if="type!=='admin'">
<div>.....</div>
<div ng-view>
</div>
</div>
</div>
So basically i have different ng-views for different views. The problem here is when user is not logged in ,login page is displayed using ng-view.But upon login the ng-view initializes with login page again (may be becoz of $route.current is already set).
When user clicks log in button he/she is directed to home page and again redirected to login page. so no change is displayed on the page.
This issue can be solved using ng-show/hide insted of ng-if. But that creates another issue that controllers are called twice becoz of two ng-view in the dom and also element can't be referred using id becoz two elements are generated fro the same id. So this solution can't be used.
Is there any way of solving it?
Multiple & Nested ng-view won't work on the same page.
You need take a look at angular ui-router which would be great to use in your case, you could also use nested ui-view using angular ui-router
Or may be you can avoid ng-if using ui-router

Clear ng-view while new contents are loading?

When I change the path of the page, my ng-view keeps its contents until the new ones are loaded by my routeProvider. Is there a way to hide them immediately and display a spinner till the new ones load?
See this fiddle regarding loading screen (can customize as necessary): http://jsfiddle.net/KmXTy/1/.
I think what you are looking for though is ng-cloak. It hides ng elements until Angular is ready for them.
<div id="template1" ng-cloak>{{ 'hello' }}</div>
<div id="template2" ng-cloak class="ng-cloak">{{ 'hello IE7' }}</div>
See https://docs.angularjs.org/api/ng/directive/ngCloak.
Also, this may be helpful: Angularjs: How to display loading-icon when using 'resolve' in $routeProvider?.

Is there a different way to hide a scope variable from showing while AngularJS is loading?

I am using this way:
<div ng-cloak>{{ message.userName || message.text }}</div>
Is this the only / best way to ensure the user does not see the {{ }} when AngularJS is still loading ?
There are several ways to hide content before Angular has a chance to run
Put the content you want to hide in another template, and use ngInclude
<div ng-include="'myPartialTemplate.html'"></div>
If you don't actually want another request made to the server to fetch another file, there are a couple of ways, as explained in the $templateCache docs. There are tools to "compile" external HTML templates into JS to avoid having to do this manually, such as grunt-angular-templates.
Similar to ngInclude, if you put everything in custom directives, with its own template, then the template content won't be shown until Angular has had a chance to run.
<my-directive></my-directive>
With a definition of:
app.directive('myDirective', function() {
return {
restrict: 'E',
template: '<div>Content hidden until Angular loaded</div>'
}
});
ngBind as an alternative to {{}} blocks
<div>Hello <span ng-bind="name"></span></div>
ngCloak as you have mentioned (in this list for completeness).
<div ng-cloak>Content hidden until Angular compiled the template</div>
But you must have certain styles loaded before the page is rendered by the browser, as explained in the ngCloak docs.
You can use ng-bind too as the documentation explains.
A typical advantage about ng-bind is the ability to provide a default value while Angular is loading (indeed, ng-cloak can only hide the content):
<p>Hello, <span ng-bind="user.name">MyDefaultValueWhileAngularIsLoading<span/></p>
Then as soon Angular is loaded, the value will be replaced by user.name.
Besides, ng-cloak is useful when dealing with blocks (many HTML lines) and ng-bind on a particular element.

Performance issue :Angular JS showing script in html page

When I call a rest service then i will get .html file from server. But it showing scripts for 1 or 2 seconds. Then it shows correct values.My problem is page loads first then controller binds data. Is there is any way to bind data before loading page?
There are basically two options that I know of:
The preferred option is to add ng-cloak to sections of the page that contain angular templates. From the docs:
<div id="template1" ng-cloak>{{ 'hello' }}</div>
<div id="template2" ng-cloak class="ng-cloak">{{ 'hello IE7' }}</div>
Use ng-bind instead of the {{ }} approach. For example:
<div ng-bind="marked4Reviewcount"></div>
Have a look at the ngCloak directive. It temporarily hides the Angular template while the page is being loaded. You could apply it on the body tag, but it's advised to do it on portions of the page so you get a progressive loading effect.
http://docs.angularjs.org/api/ng.directive:ngCloak

Resources