简体   繁体   English

Angular-ui模态问题

[英]Angular-ui modal issue

I'm trying to include an angular-ui modal in my web application but am having issues with getting everything set up. 我正在尝试在我的Web应用程序中包含一个angular-ui模式,但是我遇到了设置所有内容的问题。

The documentation indicate that you can use $modalInstance to inject the child controller into the parent controller but I don't quite understand how to go about doing so. 文档表明你可以使用$ modalInstance将子控制器注入父控制器,但我不太明白如何这样做。

Here is the current code (it is straight from the modal demo from the documentation): 这是当前的代码(它直接来自文档中的模态演示):

angular.module('myApp.controllers', []).
controller('addContent', function ($scope, $http, $modal, $log){

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

    $scope.addTerm = function () {  
        var newTerm = $modal.open({
            templateUrl: 'newTermModal.jade',
            controller: newTerms,
            resolve: {
                items: function () {
                    return $scope.items;
                }
            }
        });

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

}).
controller("newTerms",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');
    };

});

When I run the app like it is now and click the button to open the modal (addTerm function) the app crashes with the error "ReferenceError: newTerms is not defined." 当我像现在一样运行应用程序并单击按钮打开模态(addTerm函数)时,应用程序崩溃,错误“ReferenceError:newTerms未定义”。

As I mentioned above, the angular-ui site indicates you can inject a controller with $modalInstance but I have not been able to figure out how. 正如我上面提到的,angular-ui网站表明你可以使用$ modalInstance注入一个控制器,但我还是无法弄清楚如何。 a Any help would be greatly appreciated! 任何帮助将不胜感激!

EDIT 编辑

After adding the quotation marks on the pathname as suggested by Chandermani, it seems the modal is loading in the current page rather than the specified template. 在Chandermani建议的路径名上添加引号后,似乎模态正在加载当前页面而不是指定的模板。

I've changed the path to the following: templateUrl: 我已将路径更改为以下内容:templateUrl:

    $scope.addTerm = function () {  
        var newTerm = $modal.open({
            templateUrl: './views/partials/newTermModal.jade',
            controller: 'newTerms',
            resolve: {
                items: function () {
                    return $scope.items;
                }
            }
        });

A screenshot of the issue follows: 该问题的屏幕截图如下: 屏幕截图的问题

Any idea what could be causing this? 知道是什么原因引起的吗?

Well you can pass the controller as a string value. 那么你可以将控制器作为字符串值传递。 I took the default demo sample for modal and changed it to pass controller name instead of controller itself. 我为模态采用了默认的演示示例,并将其更改为传递控制器名称而不是控制器本身。

See my plunker http://plnkr.co/edit/jpJX4WvHw0SSYm3pAAzq?p=preview 看我的plunker http://plnkr.co/edit/jpJX4WvHw0SSYm3pAAzq?p=preview

So something like this 所以这样的事情

controller: 'newTerms',

should work. 应该管用。

I got the same problem, the modal loads main HTML file but not template. 我遇到了同样的问题,模态加载主HTML文件但不加载模板。

My previous configuration was: 我以前的配置是:

opens dialogs but dialog content is main HTML (like on your pic) 打开对话框,但对话框内容是主HTML (如你的图片)

$scope.opts = {
            backdrop: true,
            backdropClick: true,
            dialogFade: false,
            keyboard: true,
            templateUrl : 'app/reports/modalContent.html',
            controller : 'ModalInstanceCtrl',
            resolve: {}
        };

works as expected 按预期工作

$scope.opts = {
            backdrop: true,
            backdropClick: true,
            dialogFade: false,
            keyboard: true,
            templateUrl : '/app/reports/modalContent.html',
            controller : 'ModalInstanceCtrl',
            resolve: {}
        };

Sounds like if you put wrong templateUrl , it by default uses main page HTML. 听起来如果你把错误的templateUrl ,它默认使用主页HTML。

Be sure that you have right path for templateUrl 确保您拥有templateUrl的正确路径

Hope it will help, 希望它会有所帮助,

Have you tried to declare a dependency of 'ui.bootstrap' module? 您是否尝试声明'ui.bootstrap'模块的依赖关系? Like this: 像这样:

angular.module('myApp.controllers', ['ui.bootstrap'])

Happened to me today, too. 今天也发生在我身上。 The templateUrl in the controller must match the id for the modal in the html file. 控制器中的templateUrl必须与html文件中模式的id匹配。

You need to define the newTerms controller before your other controller. 您需要在其他控制器之前定义newTerms控制器。 Or you can change the code and just create a function inside your main controller with the name newTerms and remove the quotation marks for the name of your controller in your modal. 或者您可以更改代码,只需在主控制器中创建一个名为newTerms的函数,并在模态中删除控制器名称的引号。

$scope.addTerm = function () {  
    var newTerm = $modal.open({
        templateUrl: './views/partials/newTermModal.jade',
        controller: newTerms,
        resolve: {
            items: function () {
                return $scope.items;
            }
        }
    });

var newTerms = 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');
};
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM