【问题标题】:AngularJS open modal dialog on button clickAngularJS在按钮单击时打开模式对话框
【发布时间】:2023-03-15 22:45:01
【问题描述】:

我正在尝试做一些事情,我猜这应该很容易,但我无法弄清楚。我想要做的就是点击一个按钮打开一个模式。我正在关注这个例子。 http://fdietz.github.io/recipes-with-angular-js/common-user-interface-patterns/displaying-a-modal-dialog.html

这是我的控制器:

var app = angular.module("MyApp", ["ui.bootstrap.modal"]);

app.controller('MyCtrl', function ($scope) {
$scope.open = function () {
    $scope.showModal = true;
};

$scope.ok = function () {
    $scope.showModal = false;
};

$scope.cancel = function () {
    $scope.showModal = false;
};
});

这是我的观点:

<button class="btn" ng-click="open()">Open Modal</button>

<div modal="showModal" close="cancel()">
    <div class="modal-header">
        <h4>Modal Dialog</h4>
    </div>
    <div class="modal-body">
        <p>Example paragraph with some text.</p>
    </div>
    <div class="modal-footer">
        <button class="btn btn-success" ng-click="ok()">Okay</button>
        <button class="btn" ng-click="cancel()">Cancel</button>
    </div>
</div>

我收到错误消息 Error: [ng:areq] Argument 'MyCtrl' is not a function, got undefined.并且模式在加载时显示在页面上。提前致谢。

【问题讨论】:

  • 哼这个问题没有出现在我的电脑上。你能告诉我们更多的代码吗?
  • @John' 你还想看什么其他代码?
  • 嗯,也许你有很多代码。只是一个问题,您是否在代码中插入了指令“模态”?
  • @John' 不,我没有。它没有在示例中显示。我怎么做?抱歉,我对 AngularJS 真的很陌生。我很容易迷路:)

标签: angularjs twitter-bootstrap bootstrap-modal


【解决方案1】:

在您的第一行,您使用 "modal=" 。它是一个指令,您需要在代码中实现它。 (见这里:AngularJS reusable modal bootstrap directive

对于“Argument 'MyCtrl' is not a function, got undefined”的问题,我认为这是一个依赖问题。这里有一个类似的问题:Angularjs: Error: [ng:areq] Argument 'HomeController' is not a function, got undefined

如果你想实现一个模态对话框,我建议你在这里查看官方的 Bootstrap-Angular 文档:https://angular-ui.github.io/bootstrap/

【讨论】:

  • 好的,这修正了我的错误信息。我已经尝试了第一篇文章,结果相同。我想知道我的项目是否遗漏了一些东西,这就是它不起作用的原因?
  • 你的项目看起来不错,因为你没有错误(所以没有依赖问题)。一切都在那里。关于您的模态,您有 2 个选择:实现“模态指令”,或使用 $uibModal(更简单,请参阅文档(参见上面的链接))。代码很奇怪(专门用于模态的控制器)但很简单!跨度>
  • 谢谢。你给了我很多东西要看。我相信它会帮助我解决问题。
猜你喜欢
  • 2015-07-07
  • 1970-01-01
  • 2011-06-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-05-23
相关资源
最近更新 更多