0

I'm building an app using AngularJS. The environment already has several modules embedded in it, some of which I'm loading fine using require. I'm trying to add a novel one, and I can't get the steps right.

The module is color-picker.js: https://github.com/Alberplz/angular-colorpicker-directive

This is what I am trying, but not sure where to add the dependency. If I add it to the array in that first argument, I get "Reference error: angular not defined". I thought require() was supposed to resolve dependencies first before executing code.

<script>
    require(['angular', './js/color-picker.js', 'components/shared/index'], function (angular) {
        var app = angular.module('app', ['colorpicker']);

        app.controller('AppCtrl', ['$scope', function ($scope) {
            $scope.var3 = '#ff2e00';
            $scope.var4 = '#e6ff00';
            $scope.var5 = '#db00ff';
            $scope.var6 = '#1fd27f';
            $scope.var7 = '#008fff';
            $scope.var8 = '#ff2e00';
            $scope.var9 = '#e6ff00';
        }]);
    });
</script>

This next code works, but I do need to load other modules that pertain to this environment to get other components of the app working.

    <link rel="stylesheet" type="text/css" href="css/color-picker.min.css" />  

<script src="/scripts/lib/angular/angular.min.js"></script>
<script src="js/color-picker.min.js"></script>
<script>var app = angular.module('app', ['colorpicker']);</script>  
<script>
    app.controller('AppCtrl', ['$scope', function ($scope) {
        $scope.var3 = '#ff2e00';
        $scope.var4 = '#e6ff00';
        $scope.var5 = '#db00ff';
        $scope.var6 = '#1fd27f';
        $scope.var7 = '#008fff';
        $scope.var8 = '#ff2e00';
        $scope.var9 = '#e6ff00';
    }]);
</script>

I'm a require() noob and don't understand how to tell require that this new module is dependent on Angular.

2 Answers 2

0

Can you try this.

 <script>
        require(['angular', './js/color-picker.js', 'components/shared/index'], function (angular,colorpicker, index) {
            var app = angular.module('app', ['colorpicker']);

        app.controller('AppCtrl', ['$scope', function ($scope) {
            $scope.var3 = '#ff2e00';
            $scope.var4 = '#e6ff00';
            $scope.var5 = '#db00ff';
            $scope.var6 = '#1fd27f';
            $scope.var7 = '#008fff';
            $scope.var8 = '#ff2e00';
            $scope.var9 = '#e6ff00';
        }]);
    });
</script>
Sign up to request clarification or add additional context in comments.

5 Comments

No dice. I get that Uncaught ReferenceError: angular is not defined at color-picker.js:9
can you share little more of the error log, if you have
This got me on the right path. Thank you so much. The environment has a call to require.config() higher up in the page, and once I added another path and its dependency, it worked. Angular is pulled in with that earlier call, so this works. I'm posting the full-ish code as a new answer.
I am glad it helped :)
0

Since I don't control the whole page, I didn't realize that other dependencies were loading via an earlier call to require.config(). This is what got it all to work together.

<script>
    require.config({
        paths: {            
            'colorpicker': './js/color-picker.min'
        },
        shim: {         
            'colorpicker': {
                deps: ['angular']
            }
        }       
    }); 

    require(['angular', 'underscore', 'colorpicker'], function (angular, _) {       
        var app = angular.module('app', ['colorpicker']);
    });
</script>

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.