Solved

Angularjs ng-app question

Posted on 2015-01-06
9
130 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
Scale it in WD Gold

With up to ten times the workload capacity of desktop drives, WD Gold hard drives employ advanced technology to deliver among the best in reliability, capacity, power efficiency and performance.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Timerpicker in javascript 4 34
how can display objects ? 3 18
Accessing data in JSON response.d multiple objects 2 57
Jquery get each value 2 0
Introduction Knockoutjs (Knockout) is a JavaScript framework (Model View ViewModel or MVVM framework).   The main ideology behind Knockout is to control from JavaScript how a page looks whilst creating an engaging user experience in the least …
JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…

929 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