0

I have followed this thread steps

AngularJS directive loaded with RequireJS not compiling

but I have not yet managed to compile my code. I'm getting this error:

Error: [$injector:unpr] Unknown provider: $scopeProvider <- $scope <- radioButtonGroupDirective

These are my files:

main.js

require.config({
  baseUrl: './app',
  paths: {
    'angular': '../bower_components/angular/angular',
    'angular-route': '../bower_components/angular-route/angular-route',
    'angular-resource': '../bower_components/angular-resource/angular-resource',
    'angular-bootstrap': '../bower_components/angular-bootstrap/ui-bootstrap-tpls.min',
    'angular-text': '../bower_components/textAngular/src/textAngular',
    'angular-text-setup': '../bower_components/textAngular/src/textAngularSetup',
    'angular-text-sanitize': '../bower_components/textAngular/src/textAngular-sanitize',
    'angular-file-upload': '../bower_components/ng-file-upload/angular-file-upload.min',
    'angular-file-upload-shim': '../bower_components/ng-file-upload/angular-file-upload-shim.min'
},
  shim: {
    'angular': {'exports': 'angular'},
    'angular-route': { deps: ['angular']},
    'angular-resource': { deps: ['angular']},
    'angular-bootstrap': { deps: ['angular']},
    'angular-text': { deps: ['angular']},
    'angular-text-setup': { deps: ['angular-text']},
    'angular-text-sanitize': { deps: ['angular-text']},
    'angular-file-upload': { deps: ['angular-text']},
    'angular-file-upload-shim': { deps: ['angular-text']}
  }
});

require([
    'angular',
    'routes/routes',
    'angular-bootstrap',
    'angular-text',
    'angular-text-setup',
    'angular-text-sanitize',
    'angular-file-upload',
    'angular-file-upload-shim',
    'directives/directives'
],
function(angular){
    return angular.bootstrap(document, ["app"]);
});

modules/app.js

define(['angular', 'angular-route', 'angular-resource', 'angular-bootstrap','angular-text' , 'angular-file-upload'], function (angular)
{
    return angular.module('app', ['ngRoute', 'ngResource', 'ui.bootstrap', 'textAngular', 'angularFileUpload']);
});

routes/routes.js

define(['modules/app', 'controllers/homeCtrl', '../controllers/productsCtrl', function(app)
{
    return app.config(['$routeProvider',function($routeProvider) 
    {
        $routeProvider.when("/", {
            controller: 'homeCtrl',
            templateUrl: './app/partials/home.html'
        }).when('/productlist', {
            controller: 'productsListCtrl',
            templateUrl: './app/partials/products.html'
        });
    }]);
});

directives/directives.js

define(['modules/app'], function(app){
    app.directive('radioButtonGroup', ['$scope', function($scope){
        return {
            restrict: 'E',
            scope: { model: '=', options: '=', id: '=', name: '=', suffix: '=' },
            controller: function($scope) {
                $scope.activate = function (option, $event) {
                    $scope.model = option[$scope.id];
                    // stop the click event to avoid that Bootstrap toggles the "active" class
                    if ($event.stopPropagation) {
                        $event.stopPropagation();
                    }
                    if ($event.preventDefault) {
                        $event.preventDefault();
                    }
                    $event.cancelBubble = true;
                    $event.returnValue = false;
                };

                $scope.isActive = function(option) {
                    return option[$scope.id] == $scope.model;
                };

                $scope.getName = function (option) {
                    return option[$scope.name];
                }
            },
            template: "<button type='button' class='btn btn-{{suffix}}' " +
                "ng-class='{active: isActive(option)}'" +
                "ng-repeat='option in options' " +
                "ng-click='activate(option, $event)'>{{getName(option)}} " +
                "</button>"
        };
    }]);
});

controllers/productsCtrl.js

define(['modules/app', '../factories/cmsFactory', '../factories/api'], function(app)
{
    app.controller('productsListCtrl', ['$scope', 'UnidadNeg', 'api', function($scope, UnidadNeg, api)
    {
        $scope.myOptions = [{ key: "A", prio: "Critical" }, { key: "B", prio: "High" }, { key: "C", prio: "Medium" }, { key: "D", prio: "Low" }];
        $scope.myModel = "C";
        $scope.idProperty = "key";
        $scope.nameProperty = "prio";
        $scope.bootstrapSuffix = "info";
    }]);
});

partials/products.html

<div class="col-sm-12 radiogroup">
   Current model: {{myModel}}
   <br /><br />
   <radio-button-group class="btn-group" data-toggle="buttons-radio" model="myModel" options="myOptions" id="idProperty" name="nameProperty" suffix="bootstrapSuffix"></radio-button-group>
</div>

1 Answer 1

0

You should not inject $scope to directive. When directive's controller function is called by angular $scope, $element, $attrs and $controller params are passed to it by default. Directive controller has next signature:

controller: function ($scope, $element, $attrs, $ctrl) {}

So get rid of $scope injection here and leave next:

app.directive('radioButtonGroup', function(){ ... }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.