Angularjs controller examples

In previous post, we have seen a very simple angularjs hello world example. In this post, we are going to see about controllers.
You can consider controllers as main driver for model view changes. It is a main part of angular js application and It is javascript functions or objects which actually performs ui operation. It controls data of angularjs applications.

What is $scope?

Scope is an object which is glue between model and view. So if you want to pass data from model to view and view to model, it is done through scopes object.

How controllers and scope are related?

Actually controllers passes scope object as constructor parameter and initialise model values and functions. Please don’t worry if it sounds very confusing, once we see simple example, you will be able to relate.

Declaration of controller syntax :

You need declare module before creating controller. We will learn about module in our next tutorial.
So we use module’s controller method to declare controller.

Simple example:

Copy below text , open notepad , paste it and save it as angularJSControllerExample.html and open it in the browser.

app.js

Live Demo:

Angular js controller example

What if you don’t want to use $scope?

Yes, you can use controller as option too if you do not want to use $scope variable.
so you need to use
Advantages of using controller as options are:
  • Code becomes more readable.
  • It removes dealing with this scope and bindings.
  • There is no dependency on alias used in view(.html) and javascript

Lets take an example:

Copy below text , open notepad , paste it and save it as angularJSControllerExample.html and open it in the browser.

app.js

Live demo:

Angular js controller as example

Was this post helpful?

Related Posts

  • Multiline command powershell
    10 January

    PowerShell – Multiline Command

    Table of ContentsUsing BackticksUsing Pipeline OperatorUsing Specific Operators/Characters Using Backticks Use backticks to split multiline commands in PowerShell. [crayon-63be99fd04d47639656834/] Usually, we get an automatic line continuation in PowerShell when the command can’t syntactically be complete. However, if splitting a multiline command is required, then using backticks is preferred in such scenarios as presented above where […]

  • Call Function with Parameters in PowerShell
    08 January

    PowerShell – Call Function with Parameters

    Table of ContentsUsing Positional ParametersUsing Named Parameters Using Positional Parameters To call a function, use positional parameters separated by space. Please note that you need to pass parameters in sequence while calling the function. [crayon-63be99fd04eb4144779653/] [crayon-63be99fd04eb9408390732/] Microsoft’s PowerShell consists of a command-line shell and scripting language. It is based on the .NET framework for task […]

  • 08 January

    Press Any Key to Continue in PowerShell

    Table of ContentsSolutions for PowerShell Integrated Scripting Environment(ISE) and Command Line ConsoleUsing Read-Host CmdletUsing Message Box UISolutions for PowerShell Command Line ConsoleUsing ReadKey() Method with/without [void]Using RawUI.ReadKey() MethodUsing cmd /c pause CommandUsing timeout /t Command Solutions for PowerShell Integrated Scripting Environment(ISE) and Command Line Console Using Read-Host Cmdlet Use the Read-Host command to enable press […]

  • 08 January

    PowerShell – Unzip File

    Table of ContentsUsing Expand-Archive CmdletUsing System.IO.Compression.ZipFile NamespaceUsing 7Zip Module Using Expand-Archive Cmdlet Use the Expand-Achrive cmdlet to unzip a file/folder in PowerShell. [crayon-63be99fd05306491206435/] Please note that Expand-Archive cmdlet is available from PowerShell 5 onwards. Print envionment variable $PSVersionTable.PSVersion to check version of your PowerShell. We used the Expand-Achrive cmdlet to unzip a folder from the […]

  • PowerShell - Write log to file
    07 January

    PowerShell – Write to Log File

    Table of ContentsUsing the TranscriptUsing the Add-Content CmdletUsing the Write-Output CmdletUsing the Out-File Cmdlet Using the Transcript To write to a log file using Transcript: Use the Start-Transcript cmdlet to start writing to the log file. Perform operations normally without writing anything to the file. Use the Stop-Transcript to stop the transcript. [crayon-63be99fd054e7754420781/] [crayon-63be99fd054eb556350449/] A […]

  • Get size of folder in PowerShell
    07 January

    PowerShell – Get Size of Folder

    Table of ContentsUsing Get-ChildItem with Measure-ObjectGet File Size of a Certain TypeGet Directory Size, Including Sub-foldersUsing the PSFolderSize Module Using Get-ChildItem with Measure-Object Use Get-ChildItem with the Measure-Object cmdlet to get the size of files in the specified directory. [crayon-63be99fd056b2044166467/] [crayon-63be99fd056b5667350607/] Please note that above sum doesn’t include subfolder sizes. If you want to include […]

Leave a Reply

Your email address will not be published. Required fields are marked *

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.