Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Angularjs ng-app question

Posted on 2015-01-06
9
Medium Priority
?
153 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 2000 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

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

Question has a verified solution.

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

Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
This article discusses how to implement server side field validation and display customized error messages to the client.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn the benefit of using external CSS files and the relationship between class and ID selectors. Create your external css file by saving it as style.css then set up your style tags: (CODE) Reference the nav tag and set your prop…
Suggested Courses

636 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