0

I apologize ahead of time if this is something stupid like a semi-colon, but I'm having a hard time getting angular to recognize my controller (im a newbie to angular).

I get an error, Image Controller is undefined.

Notes: I am using the angular file upload plugin and this is a web api 2.0 application.

I have 2 Javascript files UniqueAPIStart, UniqueAPIImages

UniqueAPIStart(Fixed):

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

UniqueAPIImages(Fixed):

UniqueAPI.controller('ImageController', ['$scope', '$upload', function ($scope, $upload) {
    $scope.$watch('myFiles', function() {
        for (var i = 0; i < $scope.myFiles.length; i++) {
            var file = $scope.myFiles[i];
            $scope.upload = $upload.upload({
                url: '/api/AdminImages', // upload.php script, node.js route, or servlet url
                //method: 'POST' or 'PUT',
                //headers: {'Authorization': 'xxx'}, // only for html5
                //withCredentials: true,
                data: { myObj: $scope.myModelObj },
                file: file, // single file or a list of files. list is only for html5
                //fileName: 'doc.jpg' or ['1.jpg', '2.jpg', ...] // to modify the name of the file(s)
                //fileFormDataName: myFile, // file formData name ('Content-Disposition'), server side request form name
                // could be a list of names for multiple files (html5). Default is 'file'
                //formDataAppender: function(formData, key, val){}  // customize how data is added to the formData. 
                // See #40#issuecomment-28612000 for sample code

            }).progress(function(evt) {
                console.log('progress: ' + parseInt(100.0 * evt.loaded / evt.total) + '% file :' + evt.config.file.name);
            }).success(function(data, status, headers, config) {
                // file is uploaded successfully
                alert('file ' + config.file.name + 'is uploaded successfully. Response: ' + data);
            }).error(function (data, status) {
                alert(data.error);
            });
    //.then(success, error, progress); // returns a promise that does NOT have progress/abort/xhr functions
    //.xhr(function(xhr){xhr.upload.addEventListener(...)}) // access or attach event listeners to 
    //the underlying XMLHttpRequest
}
        /* alternative way of uploading, send the file binary with the file's content-type.
           Could be used to upload files to CouchDB, imgur, etc... html5 FileReader is needed. 
           It could also be used to monitor the progress of a normal http post/put request. 
           Note that the whole file will be loaded in browser first so large files could crash the browser.
           You should verify the file size before uploading with $upload.http().
        */
        // $scope.upload = $upload.http({...})  // See 88#issuecomment-31366487 for sample code.

    });
}]);

And then my cshtml:

@section scripts
{
    <script type="text/javascript" src ="/Scripts/API/ImageController.js"></script>
}

<div class="container" ng-app="UniqueAPI">
    <div class="row">
        <div class="col-md-4" ng-controller="ImageController">
            <form action="javascript:void(0);">
                <div class="form-group">
                    <label for="imgDescription">Email address</label>
                    <input type="text" class="form-control" id="imgDescription" placeholder="Image Description">
                    <button ng-file-select ng-model="files" multiple="true">Attach Any File</button>
                    <div ng-file-drop ng-model="files" class="drop-box"
                         drag-over-class="{accept:'dragover', reject:'dragover-err', delay:100}"
                         multiple="true" allow-dir="true" accept="image/*">
                        Drop Images here
                    </div>

                </div>
            </form>
        </div>

    </div>
</div>

LayoutFile:

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width" />
    <title>@ViewBag.Title</title>

    <script src="//ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script> 
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.1/css/bootstrap.min.css">


    @Styles.Render("~/Content/css")
    @Scripts.Render("~/bundles/modernizr")

</head>
<body>
    <div class="navbar navbar-inverse navbar-fixed-top">
        <div class="container">
            <div class="navbar-header">
                <button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse">
                    <span class="icon-bar"></span>
                    <span class="icon-bar"></span>
                    <span class="icon-bar"></span>
                </button>
                @Html.ActionLink("Application name", "Index", "Home", new { area = "" }, new { @class = "navbar-brand" })
            </div>
            <div class="navbar-collapse collapse">
                <ul class="nav navbar-nav">
                    <li>@Html.ActionLink("Home", "Index", "Home", new { area = "" }, null)</li>
                    <li>@Html.ActionLink("API", "Index", "Help", new { area = "" }, null)</li>
                </ul>
            </div>
        </div>
    </div>
    <div class="container body-content">
        @RenderBody()
        <hr />
        <footer>
            <p>&copy; @DateTime.Now.Year - My ASP.NET Application</p>
        </footer>
    </div>

    <script src="//ajax.googleapis.com/ajax/libs/angularjs/1.3.5/angular.min.js"></script>

    <script type="text/javascript" src="~/Scripts/angular-file-upload-all.js"></script>
    <script type="text/javascript" src="~/Scripts/angular-file-upload-shim.js"></script>
    <script type="text/javascript" src="~/Scripts/angular-file-upload.js"></script>
    <script type="text/javascript" src="~/Scripts/API/UniqueAPIStart.js"></script>
    @Scripts.Render("~/bundles/jquery")
    @Scripts.Render("~/bundles/bootstrap")
    @RenderSection("scripts", required: false)
</body>
</html>
5
  • 2
    You are recreating the module.. var UniqueAPI = angular.module('UniqueAPI', []); and angular.module('UniqueAPI', ['angularFileUpload']) Commented Dec 27, 2014 at 18:10
  • Where are you importing the fileUpload libraries? angular-file-upload-shim.js and angular-file-upload.js Commented Dec 27, 2014 at 18:20
  • @PSL Thanks, I fixed that. Problem is still occuring Commented Dec 27, 2014 at 18:20
  • I might have misunderstood the imports but i just imported angular-file-upload-all.js in the _Layout.cshtml file. But i didnt import the other 2. Commented Dec 27, 2014 at 18:21
  • Made fixes. Still is undefined. Commented Dec 27, 2014 at 18:26

1 Answer 1

1

First of all I could't see you importing the required libraries:

<script src="https://angular-file-upload.appspot.com/js/angular-file-upload-shim.js"></script>
<script src="https://angular-file-upload.appspot.com/js/angular-file-upload.js"></script>

and. You are creating the module twice:

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

It should be something like this

var UniqueAPI = angular.module('UniqueAPI', ['angularFileUpload']);
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you. I made the fixes but forgot to add var UniqueAPI to the start js.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.