bootstrap ui modal.open error:无法读取属性' open'未定义的

时间:2014-11-18 14:38:26

标签: angularjs angular-ui-bootstrap

我试图使用Angularjs制作我的第一个模态。我不确定或为什么我收到这个错误。我有错误:

TypeError: Cannot read property 'open' of undefined

这是我的代码: 布局:

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

    <script src="~/Scripts/angular.js" type="text/javascript"></script>
    <script src="~/Scripts/angular-resource.js" type="text/javascript"></script>
    <script src="~/Scripts/jquery-1.10.2.js" type="text/javascript"></script>
    <script src="~/Scripts/angular-route.js" type="text/javascript"></script>
    <script src="~/Scripts/ng-grid.js" type="text/javascript"></script>
    <script src="~/Scripts/bootstrap.js" type="text/javascript"></script>
    <script src="~/Scripts/angular-ui/ui-bootstrap.js" type="text/javascript"></script>



    <script src="~/Scripts/app.js" type="text/javascript"></script>

    <link rel="stylesheet" type="text/css" href="~/Content/ng-grid.css" />
    <link rel="stylesheet" type="text/css" href="~/Content/bootstrap.css" />


</head>
<body ng-controller="CtrlController">
            @RenderBody()                 
</body>
</html>

app.js:

(function() {
    var app = angular.module("app", ["ngGrid", "ui.bootstrap"]);
    app.controller("CtrlController", function ($scope, $http, $log, $modal) {




        $scope.on = function ($modal) {
            alert("dddd");
            $modal.open(
                {
                    templateUrl: 'http://localhost:58652/Home/TestView',
                    controller: "CtrlController"

                }
            );
        }
    });




})()

按钮的HTML:

<div ng-click="on()" class="gridStyle" ng-grid="gridOptions">
</div>
<button ng-click="on()" type=button class="btn">Добавить</button>

但是当我点击按钮时,我得到错误无法读取属性'open'的undefined。这是为什么?

1 个答案:

答案 0 :(得分:1)

$modal函数定义中删除$scope.on参数,它应该可以正常工作。您不必通过ngClick wince传递它,因为您已经在范围内提供$modal服务:

$scope.on = function() {
    $modal.open({
        templateUrl: 'http://localhost:58652/Home/TestView',
        controller: "CtrlController"
    });
};