【问题标题】:TypeError: Cannot read property 'open' of undefined modalTypeError:无法读取未定义模式的属性“打开”
【发布时间】:2016-02-22 03:30:08
【问题描述】:

我正在尝试显示带有特定项目信息的模式。但是当我调用该函数时,它会显示:

TypeError: Cannot read property 'open' of undefined at Scope.$scope.openModal

我希望有人能告诉我原因,这里是相关代码:

模板

<div ng-controller="View1Ctrl">
    <script type="text/ng-template" id="myModalContent.html">
        <div class="modal-header">
            <h3 class="modal-title">Item Details</h3>
        </div>
        <div class="modal-body">
            <ul ng-repeat="i in items">
                <li>Type: <span ng-bind="i.type"></span></li>
                <li>Description: <span ng-bind="i.description"></span></li>
                <li>Date: <span ng-bind="i.createDate"></span></li>
                <li>Priority: <span ng-bind="i.priority"></span></li>
                <li>Finished: <span ng-bind="i.isDone"></span></li>
            </ul>
        </div>
        <div class="modal-footer">
        <button class="btn btn-primary" ng-click="$close()">OK</button>
        </div>
    </script>
</div>

App.Js

'use strict';

// Declare app level module which depends on views, and components
angular.module('myApp', [
  //'ngRoute',
  'ui.router',
  'ui.bootstrap',
  'myApp.view1',
  'myApp.view2',
  'myApp.version',
  'myApp.items'
])
.config(function($stateProvider, $urlRouterProvider) {

    $urlRouterProvider.otherwise('/view1');

    $stateProvider  
    .state('view1', {
        url: '/view1',
        templateUrl: 'view1/view1.html',
        controller: 'View1Ctrl'
    })
    .state('view2', {
        url: '/view2',
        templateUrl: 'view2/view2.html',
        controller: 'View2Ctrl'
    });       
});

控制器

'use strict';

angular.module('myApp.view1', ['ngRoute'])
.config(['$routeProvider', function($routeProvider) {
    $routeProvider.when('/view1', {
        templateUrl: 'view1/view1.html',
        controller: 'View1Ctrl'
    });
}])
.controller('View1Ctrl', ['$scope','itemsService',function($scope,itemsService, $uiModal) {

    $scope.$on('itemSwitch.moreInfo', function(event, obj){
        $scope.openModal(obj);
    });

    $scope.openModal = function (obj) {

        var modalInstance = $uiModal.open({
            animation: $scope.animationsEnabled,
            templateUrl: 'templates/modalTemplate.html',
            controller: 'View1Ctrl',
            resolve: {
                items: function () {
                    return obj;
                }
            }
        });
    }
}]); //END

谁能指出我正确的方向来解决这个问题?

谢谢


错误消失了,但现在除了灰屏之外什么都不显示,控制台显示模态 html 正在加载但无法正常显示。

【问题讨论】:

    标签: javascript html angularjs angularjs-directive


    【解决方案1】:

    您的注入签名不正确。你错过了$uiModal。请注意以下...

    .controller('View1Ctrl', ['$scope', 'itemsService', 
        function($scope, itemsService, $uiModal) {
    

    =>

    .controller('View1Ctrl', ['$scope', 'itemsService', '$uiModal', 
        function($scope, itemsService, $uiModal) {
    

    我不完全确定您打算注入的服务的确切名称。我猜这里基于您的名为$uiModal变量,但是,我注意到docs 状态是$uibModal。您需要对此进行验证。


    针对您更新后的新发现问题,我认为templateUrl: ''&lt;script id=""&gt; 必须匹配。尝试将您的 &lt;script&gt; 标签更改为以下...

    <script type="text/ng-template" id="templates/modalTemplate.html">
        <!-- <div> -->
    

    我在玩文档中的default plunker 时偶然发现了这一点。如果两个值不相同,则会出现错误。

    【讨论】:

    • 你是对的,但名称必须更改为 uibModal,显然他们更改了它。缺点是当我单击按钮时,模式不显示(你没有错误)
    • 呵呵,我在文档中查看了这个问题后,才编辑了我的问题;)不确定为什么它可能没有显示,至少你的错误已经消失了。我可以再看看……
    • 奇怪我不确定通过查看您提供的内容。你能提供一个能重现这个问题的 plunker 吗?
    • 我尽力猜测问题出在哪里,但我有点不知所措,因为第一个问题是修复。至少看看我编辑的建议?希望对你有帮助
    猜你喜欢
    • 2015-08-18
    • 2017-01-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-11
    • 1970-01-01
    • 2020-11-29
    相关资源
    最近更新 更多