Angularjs ng-app question

Hi Experts,

I started my first Angularjs application site.
And started with an example from w3school, the angular app is started this way <body ng-app>

All the function i did work fine, and just to be clear, i didn't do any directives or modules yet, just plain function using tags scoopes.
But all the example i see use the ng-app with a name. I know that one of the goals is to have more than one app inside the html document, which not my case.

But my question is: Should i give a name to the app anyway? (<body ng-app="MyApp">)
For example how do i make a directive or a module with only <body ng-app> ?
like this?:
    var module = angular.module('', [])
        .directive('onFinishRender', function ($timeout) {
            ...
        });

Will that work?

My functions are made directly like this, with no mention of the app:
    function check_login($scope) {
        $scope.check = function (message) {
            return $scope.login;
        }
    }

What's the best practice? Will i have later problems if i don't identify my angularjs app with a name?
LVL 1
justaphaseAsked:
Who is Participating?

Improve company productivity with a Business Account.Sign Up

x
 
Alexandre SimõesConnect With a Mentor Manager / Technology SpecialistCommented:
To put it simply, yes.

If what you have is already complex and you want to use AngularJS, make sure you use it properly, otherwise it will only become even messier.
0
 
Alexandre SimõesManager / Technology SpecialistCommented:
It might look not needed for now but as any other application, yours will also grow.
Defining your application is nothing but creating a module and defining it as the "main" module by putting it in the ng-app attribute.
This will be useful as you'll need to introduce other modules for different scenarios, being the most common the Routing, Authentication, Logging... so on.

If you feel like any of the above will ever make sense to you, probably AngularJS is not the right tool for what you're doing.
It might be like using a cannon to kill an ant.

If all you're interested in is the dual-binding, have a look at knockoutjs
0
 
justaphaseAuthor Commented:
No, knockoutjs is not for me.

I already have a huge web applications made with only jquery+bootstrap+PHP, and templates using phpQuery to create the DOM. But it's not easy to implement and the code is starting to get confusing.
That's when i toke a look at Angularjs. As well as Ember and others.

And all reviews and all i have been seeing for my self tell me that this is the best framework for big apps.

So the best practice is to name my app, or in other words defining it, right?
Otherwise i can't create modules and directives?
0
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
justaphaseAuthor Commented:
Thx
0
 
justaphaseAuthor Commented:
Some code that i did that didn't work, like modules, started working when i named my app ;)
0
 
Alexandre SimõesManager / Technology SpecialistCommented:
Yep.

Your app is nothing but the main module and it's the way to encapsulate code.
Try to use directives as much as possible and have a look and AngularJS2.0 because a lot of thing will change.

For instance, the $scope will disappear along with the controllers and some other things.
A lot of the changes will break your Angular 1.3 code and even conceptually there are a lot of changes.

You can start writing code in 1.3 in a way that will make the transition smoother so make sure you take the time to look a bit ahead.

Cheers!
0
 
justaphaseAuthor Commented:
$scope will be gone?... i read the preview but didn't saw what will replace that.

Thx for heads up :)
0
 
Alexandre SimõesManager / Technology SpecialistCommented:
Actually there's no replacement, it will be gone :)

That's one reason I told you to use directives as much as possible, and make sure they don't depend on the controller.
Also you should start using the Controller as ... , and with it, stop usint the $scope inside the controllers
as shown here
0
 
justaphaseAuthor Commented:
A little late for the site i'm doing right now..
But the next one i will try to use directives and when i decide to replace the framework of my CMS i will have to make it right.. i mean very right lol..
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.