Solved

Angularjs ng-app question

Posted on 2015-01-06
9
134 Views
Last Modified: 2015-01-07
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?
0
Comment
Question by:justaphase
  • 5
  • 4
9 Comments
 
LVL 30

Expert Comment

by:Alexandre Simões
ID: 40533896
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
 
LVL 1

Author Comment

by:justaphase
ID: 40534031
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
 
LVL 30

Accepted Solution

by:
Alexandre Simões earned 500 total points
ID: 40534080
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
Are your AD admin tools letting you down?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

 
LVL 1

Author Closing Comment

by:justaphase
ID: 40534093
Thx
0
 
LVL 1

Author Comment

by:justaphase
ID: 40535202
Some code that i did that didn't work, like modules, started working when i named my app ;)
0
 
LVL 30

Expert Comment

by:Alexandre Simões
ID: 40535288
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
 
LVL 1

Author Comment

by:justaphase
ID: 40535622
$scope will be gone?... i read the preview but didn't saw what will replace that.

Thx for heads up :)
0
 
LVL 30

Expert Comment

by:Alexandre Simões
ID: 40535715
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
 
LVL 1

Author Comment

by:justaphase
ID: 40536129
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

Featured Post

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

SASS allows you to treat your CSS code in a more OOP way. Let's have a look on how you can structure your code in order for it to be easily maintained and reused.
Recently I spent hours debugging an issue in a Rails project where ActiveRecord was causing MySQL errors trying to create a User object of a class at the top level of a Single Table Inheritance model structure.  It turns out `.create` behaves differ…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
The viewer will the learn the benefit of plain text editors and code an HTML5 based template for use in further tutorials.

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question