JSFiddle - React, Tailwind, and code Playground

by jrab227

HTML

<div ng-app="myModule">
    
    <div ng-controller="badController">
        <button ng-click="behavior()">Bad Behavior</button>
    </div>
    
    <div ng-controller="cleanController1">
        <button ng-click="behavior()">Good Behavior</button>
        
    </div>
    
    <div ng-controller="cleanController2">
        <button ng-click="behavior()">Something different</button>
    </div>
    
</div>

JavaScript

angular.module('myModule', [])
.controller('badController', ['$scope',function($scope){
    //this is a bad controller I cannot extend or modify this unless I have some cool code to do it
    //Generally bad practice to begin with
    var data = ["string1", "AHHH", "string3"];
    var someObject = {
        'scream': function(){
             alert(data[1]);   
        }
    };
    
    $scope.behavior = function(){
        someObject.scream()   
    }
    
}])
//The start of cleaner code
.service('reqData1', [function(){
    //Decoupled into the module the data
    return ["string1", "AHHH", "string3"]
}])
.service('reqData2', [function(){
    //A different model if you will
    return ["string1", "Quiet", "string3"]
}])
.service('reqBehavior', [function(){
    //Decoupled into the module the behavior
    return function(coolInfo) {
        return {
            'scream': function(){
                 alert(coolInfo);   
            }
        }
    };
}])
.controller('cleanController1', ['$scope', 'reqData1', 'reqBehavior', function($scope, reqData, reqBehavior){
    //A first version
    $scope.blah = "here"
    $scope.behavior = function(){
        reqBehavior(reqData[1]).scream()   
    }
    
}])
.controller('cleanController2', ['$scope', 'reqData2', 'reqBehavior', function($scope, reqData, reqBehavior){
    //an extention if you will. code copying but you get the idea, a different setup with injections being different,
    //I can modify by injecting new stuff and using the controller for what it is... the link between the view and model
    $scope.behavior = function(){
        reqBehavior(reqData[1]).scream()   
    }
    
}])