Angular JS Magazine

marți, 16 septembrie 2014

Working With Scope-less Controllers

In this tip you can see how to use a Angular JS controller without a scope. When you think that scope is useless, you can follow the below example:

<!DOCTYPE html>
<head lang="en">
    <meta charset="UTF-8">
    <script src="../angular/angular.min.js"></script>
    <link href="../bootstrap/css/bootstrap.css" rel="stylesheet"/>
    <link href="../bootstrap/css/bootstrap-theme.css" rel="stylesheet"/>
        angular.module("appModule", [])
                .controller("appCtrl", function ($scope) {
                    this.text = "I love Angular JS";

                    this.textToUpperCase = function () {
                        this.text = this.text.toUpperCase();
<div ng-app="appModule" ng-controller="appCtrl as ctrl">
    <div class="input-group">
       <span class="input-group-btn">
         <button class="btn btn-default" type="button"
                      ng-click="ctrl.textToUpperCase()">To Upper Case</button>
        <input class="form-control" ng-model="ctrl.text">

Before pressing the button labeled "To Upper Case":

After pressing the button labeled "To Upper Case":