AngularJS - Controller As

Size
5,584 Kb
Views
12,144

How do I make an angularjs - controller as?

I wrote a simple guide to understand better the "Controller as" method, just for angular beginners!. What is a angularjs - controller as? How do you make a angularjs - controller as? This script and codes were developed by Lorenzo D'Ianni on 29 September 2022, Thursday.

AngularJS - Controller As Previews

AngularJS - Controller As - Script Codes HTML Codes

<!DOCTYPE html>
<html >
<head> <meta charset="UTF-8"> <title>AngularJS - Controller As</title> <meta name="viewport" content="width=device-width, initial-scale=1"> <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/meyer-reset/2.0/reset.min.css"> <link rel="stylesheet" href="css/style.css">
</head>
<body> <div ng-app="MyApp"> <h1>AngularJS<br>Controller as</h1> <div class="container"> <div class="explain"> <div class="explain-title">$scope could be a problem, why?</div> <div class="explain-content"> <p>Each controller has his own $scope.title, but what if we want to retrieve a title from a parent controller?</p> <pre ng-non-bindable><code>&lt;div class="box" ng-controller="MainCtrl"&gt;	&lt;div class="box-title"&gt;MainCtrl&lt;/div&gt;	&lt;div class="box-title--bind"&gt;{{title}}&lt;/div&gt;	&lt;div class="box" ng-controller="SecondCtrl"&gt;	&lt;div class="box-title"&gt;SecondCtrl&lt;/div&gt;	&lt;div class="box-title--bind"&gt;{{title}}&lt;/div&gt;	&lt;div class="box-title--bind"&gt;{{title}}&lt;/div&gt;	&lt;div class="box" ng-controller="ThirdCtrl"&gt;	&lt;div class="box-title"&gt;ThirdCtrl&lt;/div&gt;	&lt;div class="box-title--bind"&gt;{{title}}&lt;/div&gt;	&lt;div class="box-title--bind"&gt;{{title}}&lt;/div&gt;	&lt;div class="box-title--bind"&gt;{{title}}&lt;/div&gt;	&lt;/div&gt; <c>&lt;!-- // ThirdCtrl --&gt;</c>	&lt;/div&gt; <c>&lt;!-- // SecondCtrl --&gt;</c>
&lt;/div&gt; <c>&lt;!-- // MainCtrl --&gt;</c>	</code></pre> </div> </div> <div class="box" ng-controller="MainCtrl"> <div class="box-title">MainCtrl</div> <div class="box-title--bind">{{title}}</div> <div class="box" ng-controller="SecondCtrl"> <div class="box-title">SecondCtrl</div> <div class="box-title--bind">{{title}}</div> <div class="box-title--bind">{{title}}</div> <div class="box" ng-controller="ThirdCtrl"> <div class="box-title">ThirdCtrl</div> <div class="box-title--bind">{{title}}</div> <div class="box-title--bind">{{title}}</div> <div class="box-title--bind">{{title}}</div> </div> </div> </div> </div> <div class="container"> <div class="explain"> <div class="explain-title">Dirty solution: $scope and $parent.$scope</div> <div class="explain-content"> <p> Is $parent.$scope a solution? Yes and no at the same time. Can you image it with 4, 5 or 6 nested controllers with a lot of tags and nested tags? It is impossible to figure out which is the controller that refers to that particular $scope or $parent.$scope. </p> <pre ng-non-bindable><code>
&lt;div class="box" ng-controller="MainCtrl"&gt;	&lt;div class="box-title"&gt;MainCtrl&lt;/div&gt;	&lt;div class="box-title--bind"&gt;{{title}}&lt;/div&gt;	&lt;div class="box" ng-controller="SecondCtrl"&gt;	&lt;div class="box-title"&gt;SecondCtrl&lt;/div&gt;	&lt;div class="box-title--bind"&gt;{{$parent.title}}&lt;/div&gt;	&lt;div class="box-title--bind"&gt;{{title}}&lt;/div&gt;	&lt;div class="box" ng-controller="ThirdCtrl"&gt;	&lt;div class="box-title"&gt;ThirdCtrl&lt;/div&gt;	&lt;div class="box-title--bind"&gt;{{$parent.$parent.title}}&lt;/div&gt;	&lt;div class="box-title--bind"&gt;{{$parent.title}}&lt;/div&gt;	&lt;div class="box-title--bind"&gt;{{title}}&lt;/div&gt;	&lt;/div&gt; <c>&lt;!-- // ThirdCtrl --&gt;</c>	&lt;/div&gt; <c>&lt;!-- // SecondCtrl --&gt;</c>
&lt;/div&gt; <c>&lt;!-- // MainCtrl --&gt;</c>	</code></pre> </div> </div> <div class="box" ng-controller="MainCtrl"> <div class="box-title">MainCtrl</div> <div class="box-title--bind">{{title}}</div> <div class="box" ng-controller="SecondCtrl"> <div class="box-title">SecondCtrl</div> <div class="box-title--bind">{{$parent.title}}</div> <div class="box-title--bind">{{title}}</div> <div class="box" ng-controller="ThirdCtrl"> <div class="box-title">ThirdCtrl</div> <div class="box-title--bind">{{$parent.$parent.title}}</div> <div class="box-title--bind">{{$parent.title}}</div> <div class="box-title--bind">{{title}}</div> </div> </div> </div> </div> <div class="container"> <div class="explain" ng-init="tab = 1"> <div class="explain-title">Perfect solution: Controller as</div> <div class="explain-content"> <p>You have to declare "Controller as ctrl" and also in your .js use "this" instead $scope to bind the view/model! Save your time without search the parent controller, write immediately what you need, it's easy and fast.</p> <nav class="tabs-triggers"> <a href="#!" class="tab-trigger" ng-click="tab = 1" ng-class="{'is-active': tab === 1}">HTML</a> <a href="#!" class="tab-trigger" ng-click="tab = 2" ng-class="{'is-active': tab === 2}">Javascript</a> </nav> <div ng-show="tab === 1"> <pre ng-non-bindable><code>
&lt;div class="box" ng-controller="MainCtrl <b>as main</b> "&gt;	&lt;div class="box-title"&gt;MainCtrl&lt;/div&gt;	&lt;div class="box-title--bind"&gt;{{<u>main</u>.title}}&lt;/div&gt;	&lt;div class="box" ng-controller="SecondCtrl <b>as second</b> "&gt;	&lt;div class="box-title"&gt;SecondCtrl&lt;/div&gt;	&lt;div class="box-title--bind"&gt;{{<u>main</u>.title}}&lt;/div&gt;	&lt;div class="box-title--bind"&gt;{{<u>second</u>.title}}&lt;/div&gt;	&lt;div class="box" ng-controller="ThirdCtrl <b>as third</b> "&gt;	&lt;div class="box-title"&gt;ThirdCtrl&lt;/div&gt;	&lt;div class="box-title--bind"&gt;{{<u>main</u>.title}}&lt;/div&gt;	&lt;div class="box-title--bind"&gt;{{<u>second</u>.title}}&lt;/div&gt;	&lt;div class="box-title--bind"&gt;{{<u>third</u>.title}}&lt;/div&gt;	&lt;/div&gt; <c>&lt;!-- // ThirdCtrl --&gt;</c>	&lt;/div&gt; <c>&lt;!-- // SecondCtrl --&gt;</c>
&lt;/div&gt; <c>&lt;!-- // MainCtrl --&gt;</c>	</code></pre> </div> <div ng-show="tab === 2"> <pre ng-non-bindable><code>
angular	.module('MyApp', [])	.controller('MainCtrl', [function MainCtrl(){ var main = this; main.title = "Main Ctrl Title"	}])	.controller('SecondCtrl', [function SecondCtrl(){ var second = this; second.title = "Second Ctrl Title"	}])	.controller('ThirdCtrl', [function ThirdCtrl(){ var third = this; third.title = "Third Ctrl Title"	}]);	</code></pre> </div> </div> </div> <div class="box" ng-controller="MainCtrl as main"> <div class="box-title">MainCtrl</div> <div class="box-title--bind">{{main.title}}</div> <div class="box" ng-controller="SecondCtrl as second"> <div class="box-title">SecondCtrl</div> <div class="box-title--bind">{{main.title}}</div> <div class="box-title--bind">{{second.title}}</div> <div class="box" ng-controller="ThirdCtrl as third"> <div class="box-title">ThirdCtrl</div> <div class="box-title--bind">{{main.title}}</div> <div class="box-title--bind">{{second.title}}</div> <div class="box-title--bind">{{third.title}}</div> </div> </div> </div> </div> <h2>Lorenzo D'Ianni</h2>
</div>	<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.3/angular.min.js"></script>	<script src="//cdnjs.cloudflare.com/ajax/libs/highlight.js/8.7/highlight.min.js"></script>	<link rel="stylesheet" href="//cdnjs.cloudflare.com/ajax/libs/highlight.js/8.7/styles/googlecode.min.css">
<script>hljs.initHighlightingOnLoad();</script> <script src="js/index.js"></script>
</body>
</html>

AngularJS - Controller As - Script Codes CSS Codes

@import url(https://fonts.googleapis.com/css?family=Roboto:300,400,500);
*, *:before, *:after { margin: 0; padding: 0; -webkit-overflow-scrolling: touch; -webkit-font-smoothing: antialiased; -moz-osx-font-smoothing: grayscale; box-sizing: border-box;
}
body { font-family: 'Roboto', sans-serif; font-weight: 400; font-size: 14px; margin: 0 auto; padding: 32px 5%; border-top: 4px solid #03A9F4; border-bottom: 8px solid #03A9F4; color: rgba(0, 0, 0, 0.73);
}
h1, h2 { text-align: center;
}
h1 { font-size: 300%; font-weight: 300; line-height: 120%; color: #03A9F4;
}
h2 { font-size: 12px; text-transform: uppercase; font-weight: 500; opacity: .3; margin: 72px 0 32px; letter-spacing: 8px;
}
.container { position: relative; padding: 48px 0; border-bottom: 1px solid rgba(0, 0, 0, 0.04);
}
.container:before, .container:after { content: ""; display: block;
}
.container:after { clear: both;
}
.container:before { position: absolute; bottom: 0; left: 50%; width: 12px; height: 12px; background-color: #efefef; -webkit-transform: translate(-50%, 50%); transform: translate(-50%, 50%); border-radius: 50%;
}
.box { padding: 24px; margin: 48px 0 12px; border-radius: 3px; box-shadow: inset 0 0 1px 0px rgba(0, 0, 0, 0.08), inset 0 0 5px 0px rgba(0, 0, 0, 0.08), inset 0 0 20px 0px rgba(0, 0, 0, 0.05);
}
.box-title, .explain-title { font-weight: 500; font-size: 16px; line-height: 130%; padding-bottom: 8px; margin-bottom: 8px; border-bottom: 1px solid rgba(0, 0, 0, 0.04);
}
.box-title--bind { margin-bottom: 12px;
}
.box > .box { float: none; width: 100%; display: inline-block; vertical-align: top; margin: 12px 0 0; background-color: rgba(0, 0, 0, 0.04);
}
.box > .box-title--bind:last-child { margin-bottom: 0;
}
.explain { padding: 0;
}
.explain-title { border-bottom-color: #03A9F4;
}
.explain-content { line-height: 150%;
}
.explain pre { position: relative; font-family: monospace; font-size: 12px; line-height: normal; overflow: auto; opacity: 0.73;
}
.explain pre code { padding-bottom: 0 !important;
}
.explain pre b, .explain pre u { position: relative; z-index: 1;
}
.explain pre b:before, .explain pre u:before { content: ""; position: absolute; z-index: -1; top: 50%; left: 50%; width: 120%; height: 120%; -webkit-transform: translate(-50%, -50%); transform: translate(-50%, -50%);
}
.explain pre b:before { width: 120%; background-color: red; border-radius: 100px;
}
.explain pre b *, .explain pre b *:before, .explain pre b *:after { color: white !important;
}
.explain pre u { text-decoration: none !important;
}
.explain pre u:before { width: 100%; border-bottom: 2px solid red;
}
.explain pre c *, .explain pre c *:before, .explain pre c *:after { color: rgba(128, 128, 128, 0.5);
}
.tabs-triggers { font-size: 0; text-align: center;
}
.tabs-triggers .tab-trigger { position: relative; z-index: 2; font-size: 14px; display: inline-block; vertical-align: top; width: 50%; cursor: pointer; padding: 8px; text-decoration: none; color: inherit; border-bottom: 1px solid rgba(0, 0, 0, 0.04);
}
.tabs-triggers .tab-trigger.is-active { color: #03A9F4; border-bottom: 1px solid #03A9F4;
}
.tabs-triggers .tab-trigger.is-active:after { position: absolute; content: ""; bottom: 0; left: 50%; width: 0; height: 0; border-left: 10px solid transparent; border-right: 10px solid transparent; border-top: 6px solid #03A9F4; -webkit-transform: translate(-50%, 100%); transform: translate(-50%, 100%);
}
@media screen and (min-width: 800px) { h1 { font-size: 500%; letter-spacing: -2px; } .box, .explain { float: left; } .box { width: 40%; margin: 0; } .explain { width: 60%; padding: 24px 5% 0; }
}

AngularJS - Controller As - Script Codes JS Codes

angular.module('MyApp', [])
.controller('MainCtrl', ['$scope', function MainCtrl($scope){ var main = this; main.title = "Main Ctrl Title" $scope.title = "Main Ctrl Title"
}])
.controller('SecondCtrl', ['$scope', function SecondCtrl($scope){ var second = this; second.title = "Second Ctrl Title" $scope.title = "Second Ctrl Title"
}])
.controller('ThirdCtrl', ['$scope', function ThirdCtrl($scope){ var third = this; third.title = "Third Ctrl Title" $scope.title = "Third Ctrl Title"
}]);
AngularJS - Controller As - Script Codes
AngularJS - Controller As - Script Codes
Home Page Home
Developer Lorenzo D'Ianni
Username lorenzodianni
Uploaded September 29, 2022
Rating 3
Size 5,584 Kb
Views 12,144
Do you need developer help for AngularJS - Controller As?

Find the perfect freelance services for your business! Fiverr's mission is to change how the world works together. Fiverr connects businesses with freelancers offering digital services in 500+ categories. Find Developer!

Lorenzo D'Ianni (lorenzodianni) Script Codes
Create amazing Facebook ads with AI!

Jasper is the AI Content Generator that helps you and your team break through creative blocks to create amazing, original content 10X faster. Discover all the ways the Jasper AI Content Platform can help streamline your creative workflows. Start For Free!