Solved

Angularjs ng-app question

Posted on 2015-01-06
9
125 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
 
LVL 1

Author Closing Comment

by:justaphase
ID: 40534093
Thx
0
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 
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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

I found this questions asking how to do this in many different forums, so I will describe here how to implement a solution using PHP and AJAX. The logical flow for the problem should be: Write an event handler for the first drop down box to get …
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
HTML5 has deprecated a few of the older ways of showing media as well as offering up a new way to create games and animations. Audio, video, and canvas are just a few of the adjustments made between XHTML and HTML5. As we learned in our last micr…

743 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now