angularjs controller in PartialView not working

http://stackoverflow.com/questions/25043436/angularjs-controller-in-partialview-not-working/25043850#25043850

by Eduardo GarcĂ­a Sanz

HTML

<div ng-controller="Main">
    <a href="" ng-click="show()">Select a color</a>
</div>

CSS

.modal {
    position: fixed;
    top: 0;
    right: 0;
    bottom: 0;
    left: 0;
    background-color: rgba(0, 0, 0, .5);
}

.modal > .win {
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    padding: 20px;
    background-color: #fff;
    min-width: 200px;
}

.modal > .win > a.close {
    position: absolute;
    top: 5px;
    right: 5px;
    width: 16px;
    height: 16px;
    background: transparent url(https://cdn1.iconfinder.com/data/icons/mimiGlyphs/16/close_delete.png) no-repeat center;
}

.colors a {
    display: block;
    padding: 5px;
    border-left: 10px solid transparent;
}

JavaScript

var app = angular.module('app', []);

app.controller('Main', function($scope, HttpService) {
    
    var url = 'https://gist.githubusercontent.com/coma/1a77d6632edf5426fcd8/raw/73bf3c9549256710bb1c23bbc27bf10c8aa694ac/colors.html';
    var data = {
        colors: [
            {name: 'red', hex: '#f00'},
            {name: 'green', hex: '#0f0'},
            {name: 'blue', hex: '#00f'}
        ]
    };

    $scope.show = function () {
        
        HttpService.getModal(url, data);
    };
});

app.controller('Modal', function($scope) {

    $scope.select = function (color) {
        
        alert(color.name);
        $scope.close();
    };
});

app.factory('HttpService', function($document, $compile, $rootScope, $templateCache, $http) {

    var body = $document.find('body');

    return {
        getModal: function (url, data) {

            // A new scope for the modal using the passed data
            var scope = $rootScope.$new();
            angular.extend(scope, data);

            // Caching the template for future calls
            var template = $http.get(url, {cache: $templateCache})
                .then(function (response) {

                    // Wrapping the template with some extra markup
                    var modal = angular.element([
                        '<div class="modal">',
                        '<div class="win">',
                        '<a href="#" class="close" ng-click="close()"></a>',
                        '<div>' + response.data + '</div>',
                        '</div>',
                        '</div>'
                    ].join(''));

                    // The important part
                    $compile(modal)(scope);
                    // Adding the modal to the body
                    body.append(modal);

                    // A close method
                    scope.close = function () {

                        modal.remove();
                        scope.$destroy();
                    };
               ...