2016-08-25 12 views
0

AngularUI Modalを初めて試してみました。AngularUI Modalコントローラエラー

一定のエラーがあります。controller as vmは関数ではありません。未定義です。ここで

は私の一部です:

<div class="container" ng-controller="controller as vm"> 
<script type="text/ng-template" id="hello.html"> 
    <div class="modal-header"> 
    <h3 class="modal-title" id="modal-title">I'm a modal!</h3> 
    </div> 
    <div class="modal-body" id="modal-body"> 
    hello 
    </div> 
    <div class="modal-footer"> 
    <button class="btn btn-primary" type="button" ng-click="vm.ok()">OK</button> 
    <button class="btn btn-warning" type="button" ng-click="vm.cancel()">Cancel</button> 
    </div> 
</script> 

<button type="button" class="btn btn-default" ng-click="vm.open()">Open me!</button> 

はコントローラー:

app.controller('controller',[function($uibModal,$log) 
{ 
var vm = this; 
vm.animationsEnabled = true; 

vm.open = function() { 
var modalInstance = $uibModal.open({ 
    animation: vm.animationsEnabled, 
    ariaLabelledBy: 'modal-title', 
    ariaDescribedBy: 'modal-body', 
    templateUrl: 'hello.html', 
    controller: 'Modalcontroller', 
    //controllerAs: 'vm', 


    }) 
}; 
}]); 

app.js

var app=angular.module("myapp",['ui.router', 'ui.bootstrap']); 

... 

    .state("user", { 
    url: "/user", 
    views: { 
     "main": { 
     templateUrl: "partials/user.html", 
     controller: "controller", 
     controllerAs: "vm", 
     } 
    } 
}); 
+1

どの角度の角度を使用していますか?そして、なぜあなたは囲みページビューとモーダルに同じコントローラを使用していますか?それはあまり意味がありません。さらに、コントローラはすでに状態定義で指定されているため、ビューで再度指定すると、1つではなく2つのインスタンスが作成されます。ビューから削除します。 –

+0

私は角度1.5を使用しており、あなたが言ったようにそれに応じて編集しました。 – uttejh

+1

'[function($ uibModal、$ log)'を 'function($ uibModal、$ log)'または '['$ uibModal'、 '$ log'、または' $ uibModal、$ log 'で置き換えてください。 function($ uibModal、$ log) 'となります。最初のものを事前に作成し、ng-annotateを使用して、縮小が必要な場合はビルド中に自動的に2番目のフォームに変換します。 –

答えて

0

私はplunkrを作成しました。どうぞご覧ください

(https://plnkr.co/edit/6fx26BVrXu0ud8TkvrMq?p=preview) 

myApp.controller('ModalController', ['$scope', '$uibModalInstance', '$uibModal', '$state', function($scope, $uibModalInstance, $uibModal, $state) { 
     var self = this; 
     self.cancel = function(){ 
      $uibModalInstance.dismiss(); 
     }; 

    }]); 
    myApp.controller('userController', ['$scope', '$uibModal', '$state', function($scope, $uibModal, $state) { 
     var self = this; 
     self.animationsEnabled = true; 

     self.open = function(){ 

     var modalInstance = $uibModal.open({ 
       animation: self.animationsEnabled, 
       ariaLabelledBy: 'modal-title', 
       ariaDescribedBy: 'modal-body', 
       templateUrl: 'hello.html', 
       controller: 'ModalController', 
       controllerAs: 'vm' 

      }); 
     }; 
    }]); 
関連する問題