2017-10-28 76 views
1

嗨我只是试图让这个工作,但不断收到错误。当按钮被点击时,它应该向屏幕添加警报。角js添加警报ctrl不工作

var app = angular.module('ui.bootstrap') 
 
    
 
    angular.module('app').controller('messageCtrl', function ($scope) { 
 
    $scope.alerts = [ 
 
    { type: 'danger', msg: 'Oh snap! Change a few things up and try submitting again.' }, 
 
    { type: 'success', msg: 'Well done! You successfully read this important alert message.' } 
 
    ]; 
 

 
    $scope.addAlert = function() { 
 
    $scope.alerts.push({msg: 'Another alert!'}); 
 
    }; 
 

 
    $scope.closeAlert = function(index) { 
 
    $scope.alerts.splice(index, 1); 
 
    }; 
 
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> 
 
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script> 
 
<body ng-app="app" ng-controller="messageCtrl"> 
 
<div class="container"> 
 

 
    
 
    
 
<script type="text/ng-template" id="alert.html"> 
 
    <div ng-transclude></div> 
 
    </script> 
 

 
    <div uib-alert ng-repeat="alert in alerts" ng-class="'alert-' + (alert.type || 'warning')" close="closeAlert($index)">{{alert.msg}}</div> 
 
    <div uib-alert template-url="alert.html" style="background-color:#fa39c3;color:white">A happy alert!</div> 
 
    <button type="button" class='btn btn-default' ng-click="addAlert()">Add Alert</button> 
 
</div>

回答

1

请试试这个代码。它的工作对我来说。

<!DOCTYPE html> 
<html> 
<head> 
    <title></title> 
    <meta charset="utf-8" /> 
</head> 

    <body ng-app="app" ng-controller="messageCtrl"> 
     <div class="container"> 

      <script type="text/ng-template" id="alert.html"> 
       <div ng-transclude></div> 
      </script> 

      <div uib-alert ng-repeat="alert in alerts" ng-class="'alert-' + (alert.type || 'warning')" close="closeAlert($index)">{{alert.msg}}</div> 
      <div uib-alert template-url="alert.html" style="background-color:#fa39c3;color:white">A happy alert!</div> 
      <button type="button" class='btn btn-default' ng-click="addAlert()">Add Alert</button> 
     </div> 


     <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> 
     <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script> 
     <script> 
      // var app = angular.module('ui.bootstrap') 

      angular.module('app',[]).controller('messageCtrl', function ($scope) { 
       $scope.alerts = [ 
        { type: 'danger', msg: 'Oh snap! Change a few things up and try submitting again.' }, 
        { type: 'success', msg: 'Well done! You successfully read this important alert message.' } 
       ]; 

       $scope.addAlert = function() { 
        $scope.alerts.push({ msg: 'Another alert!' }); 
       }; 

       $scope.closeAlert = function (index) { 
        $scope.alerts.splice(index, 1); 
       }; 
      }); 
     </script> 
    </body> 
</html> 

所做的修改如下。

评论此行,因为它没有任何意义。

var app = angular.module('ui.bootstrap') 

更正了模块的语法。

angular.module('app',[]) 
+0

为什么ui bootstrap不需要? – Vzupo

+0

所有警报都是引导程序 – Vzupo

+0

在目前的情况下,它没有任何意义。语法缺少第二个参数,它是依赖关系列表。你从哪里得到这条线? –