2015-10-09 12 views
9

Ich bin wirklich neu in Angular. Ich versuche das Modal Sample unter diesem Link zu erstellen Ich habe kein Glück damit! Ich habe einen Plünderer http://plnkr.co/edit/018Ed7RG3Y0GoAlK7a14?p=catalogue Ich muss nur in der Lage sein, ein Modal auf einen Klick zu öffnen. Ich erhalte die Fehlermeldung Fehler: [ng: AREQ] Argument 'ModalDemoCtrl' ist keine Funktion, nicht definiert wurdeAngularJS Modal Popup

hier ist meine Ansicht

<div ng-controller="ModalDemoCtrl"> 
<script type="text/ng-template" id="myModalContent.html"> 
    <div class="modal-header"> 
     <h3 class="modal-title">I'm a modal!</h3> 
    </div> 
    <div class="modal-body"> 
     <ul> 
      <li ng-repeat="item in items"> 
       <a href="#" ng-click="$event.preventDefault(); selected.item = item">{{ item }}</a> 
      </li> 
     </ul> 
     Selected: <b>{{ selected.item }}</b> 
    </div> 
    <div class="modal-footer"> 
     <button class="btn btn-primary" type="button" ng-click="ok()">OK</button> 
     <button class="btn btn-warning" type="button" ng-click="cancel()">Cancel</button> 
    </div> 
</script> 
<button type="button" class="btn btn-default" ng-click="open()">Open me!</button> 
<button type="button" class="btn btn-default" ng-click="open('lg')">Large modal</button> 
<button type="button" class="btn btn-default" ng-click="open('sm')">Small modal</button> 
<button type="button" class="btn btn-default" ng-click="toggleAnimation()">Toggle Animation ({{ animationsEnabled }})</button> 
<div ng-show="selected">Selection from a modal: {{ selected }}</div> 

Hier ist mein Controller:

angular.module('crm.ma', ['ngAnimate', 'ui.bootstrap']); 
angular.module('crm.ma').controller('ModalDemoCtrl', ModalDemoCtrl, function ($scope, $uibModal, $log) { 

$scope.items = ['item1', 'item2', 'item3']; 

$scope.animationsEnabled = true; 

$scope.open = function (size) { 

    var modalInstance = $uibModal.open({ 
     animation: $scope.animationsEnabled, 
     templateUrl: 'myModalContent.html', 
     controller: 'ModalInstanceCtrl', 
     size: size, 
     resolve: { 
      items: function() { 
       return $scope.items; 
      } 
     } 
    }); 

    modalInstance.result.then(function (selectedItem) { 
     $scope.selected = selectedItem; 
    }, function() { 
     $log.info('Modal dismissed at: ' + new Date()); 
    }); 
}; 

$scope.toggleAnimation = function() { 
    $scope.animationsEnabled = !$scope.animationsEnabled; 
}; 

});

angular.module('crm.ma').controller('ModalInstanceCtrl', ModalInstanceCtrl, function ($scope, $modalInstance, items) { 

$scope.items = items; 
$scope.selected = { 
    item: $scope.items[0] 
}; 

$scope.ok = function() { 
    $modalInstance.close($scope.selected.item); 
}; 

$scope.cancel = function() { 
    $modalInstance.dismiss('cancel'); 
}; 
}); 
+0

Ihre plunkr ist gebrochen. –

+0

@PankajParkar tut mir leid. Ich habe den Code in die Datei index.html gelegt. Sie sollten es jetzt sehen können. – hollyquinn

+0

Sie müssen 'angular.js & ui-bootstrap.js' vor Ihrer 'script.js' einschließen –

Antwort

8

Hier ist eine korrigierte Gabel Ihrer zupfen: http://plnkr.co/edit/6djuhA8ohMkrWW7zohg1?p=preview. Sie hatten nur geringfügige Syntaxfehler.

JAVASCRIPT

var app = angular.module('crm.ma', ['ngAnimate', 'ui.bootstrap']); 

app.controller('ModalDemoCtrl', function ($scope, $uibModal, $log) { 

$scope.items = ['item1', 'item2', 'item3']; 

$scope.animationsEnabled = true; 

$scope.open = function (size) { 

    var modalInstance = $uibModal.open({ 
     animation: $scope.animationsEnabled, 
     templateUrl: 'myModalContent.html', 
     controller: 'ModalInstanceCtrl', 
     size: size, 
     resolve: { 
      items: function() { 
       return $scope.items; 
      } 
     } 
    }); 

    modalInstance.result.then(function (selectedItem) { 
     $scope.selected = selectedItem; 
    }, function() { 
     $log.info('Modal dismissed at: ' + new Date()); 
    }); 
}; 

    $scope.toggleAnimation = function() { 
    $scope.animationsEnabled = !$scope.animationsEnabled; 
    }; 

}); 

// Please note that $modalInstance represents a modal window (instance) dependency. 
// It is not the same as the $uibModal service used above. 

app.controller('ModalInstanceCtrl', function ($scope, $modalInstance, items) { 

    $scope.items = items; 
    $scope.selected = { 
    item: $scope.items[0] 
    }; 

    $scope.ok = function() { 
    $modalInstance.close($scope.selected.item); 
    }; 

    $scope.cancel = function() { 
    $modalInstance.dismiss('cancel'); 
    }; 
}); 

HTML

<!DOCTYPE html> 
<html data-ng-app="crm.ma"> 

<head> 
<link data-require="[email protected]" data-semver="3.1.1" rel="stylesheet" href="//netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.min.css" /> 
<link rel="stylesheet" href="style.css" /> 
<script src="//ajax.googleapis.com/ajax/libs/angularjs/1.4.6/angular.js"></script> 
<script src="//ajax.googleapis.com/ajax/libs/angularjs/1.4.6/angular-animate.js"></script> 
<script src="//angular-ui.github.io/bootstrap/ui-bootstrap-tpls-0.14.0.js"></script> 
<script src="ModalDemoCtrl.js"></script> 
</head> 

<body> 
    <div ng-controller="ModalDemoCtrl"> 
    <script type="text/ng-template" id="myModalContent.html"> 
     <div class="modal-header"> 
      <h3 class="modal-title">I'm a modal!</h3> 
     </div> 
     <div class="modal-body"> 
     <ul> 
      <li ng-repeat="item in items"> 
       <a href="#" ng-click="$event.preventDefault(); selected.item = item">{{ item }}</a> 
      </li> 
     </ul> 
     Selected: <b>{{ selected.item }}</b> 
     </div> 
     <div class="modal-footer"> 
     <button class="btn btn-primary" type="button" ng-click="ok()">OK</button> 
     <button class="btn btn-warning" type="button" ng-click="cancel()">Cancel</button> 
     </div> 
    </script> 
    <button type="button" class="btn btn-default" ng-click="open()">Open me!</button> 
    <button type="button" class="btn btn-default" ng-click="open('lg')">Large modal</button> 
    <button type="button" class="btn btn-default" ng-click="open('sm')">Small modal</button> 
    <button type="button" class="btn btn-default" ng-click="toggleAnimation()">Toggle Animation ({{ animationsEnabled }})</button> 
    <div ng-show="selected">Selection from a modal: {{ selected }}</div> 
    </div> 
    </body> 

</html> 
+0

Ich habe meinen Code so geändert, dass er zu deinem passt. Ich bekomme immer noch keine Fehler, aber wenn ich meinen Controller der Seite index.html hinzufüge, kommt er ohne Fehler und eine leere Seite zurück. Irgendeine Idee, was ich falsch mache? – hollyquinn

+0

@hollyquinn, Hast du das funktioniert? Entschuldigung, ich war ein paar Tage auf ärztliche Behandlung. – steveo

5

Sie müssen diese Zeile beheben:

angular.module('crm.ma').controller('ModalDemoCtrl', ModalDemoCtrl, function ($scope, $uibModal, $log) {  
// what is this, huh? ------------------------------------^ 

Korrekter Code:

angular.module('crm.ma').controller('ModalDemoCtrl', function ($scope, $uibModal, $log) { 

Sie haben ähnliches Problem mit ModalInstanceCtrl.

Sie auch ng-app="crm.ma" Attribut fehlt.

Demo:http://plnkr.co/edit/VDhDAHM2beVtYYsJBXoi?p=preview

+0

Ich habe die Codezeile korrigiert, aber es funktioniert immer noch nicht. ng-app ist in meiner eigentlichen Anwendung enthalten. Dies ist eine riesige Anwendung, ich habe nur die Seiten hinzugefügt, über die ich eine Frage zu Plunker habe. – hollyquinn

+0

Welchen Fehler bekommen Sie? – dfsq

+0

ReferenceError: ModalInstanceCtrl ist nicht definiert (anonyme Funktion) @ ModalDemoCtrl.js: 36 angular.js: 68 Uncaught Fehler: [$ injector: modulerr] Fehler beim Instanziieren des Moduls crm.ma aufgrund von: Fehler: [$ injector: modulerr ] Fehler beim Instanziieren des Moduls ngAnimate aufgrund von: Fehler: [$ injector: nomod] Das Modul 'ngAnimate' ist nicht verfügbar! Sie haben entweder den Modulnamen falsch geschrieben oder vergessen, ihn zu laden. Wenn Sie ein Modul registrieren, stellen Sie sicher, dass Sie die Abhängigkeiten als zweites Argument angeben. danke für Ihre Hilfe! – hollyquinn