在单独的 js 文件中带有控制器的 Angular ui 模式 [英] Angular ui modal with controller in separate js file

查看:21
本文介绍了在单独的 js 文件中带有控制器的 Angular ui 模式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试制作一个可以从应用程序中的多个位置实例化的模式.从这里给出的例子中:用于 Bootstrap 的 Angular 指令 模态控制器在同一个文件作为实例化模态的控制器.我想将模态控制器与应用程序"控制器分开.

index.html:

<html ng-app="modalTest"><头><link rel="stylesheet" type="text/css" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css"><body ng-controller="modalTestCtrl"><button ng-click="openModal()">Modal</button><script type="text/javascript" src="http://cdnjs.cloudflare.com/ajax/libs/angular.js/1.2.20/angular.min.js"></script><script type="text/javascript" src="http://cdnjs.cloudflare.com/ajax/libs/angular-ui-bootstrap/0.10.0/ui-bootstrap.min.js"></脚本><script type="text/javascript" src="http://cdnjs.cloudflare.com/ajax/libs/angular-ui-bootstrap/0.10.0/ui-bootstrap-tpls.min.js"><;/脚本><script type="text/javascript" src="modalTest.js"></script></html>

控制器:

var app = angular.module('modalTest', ['ui.bootstrap']);app.controller('modalTestCtrl', ['$scope', '$modal', function($scope, $modal) {$scope.openModal = function() {var modalInstance = $modal.open({templateUrl: 'modal.html',控制器:'modalCtrl',尺寸:'sm'});}}]);//我想在另一个 JavaScript 文件中使用它...app.controller('modalCtrl', ['$scope', function($scope) {$scope.hello = '作品';}]);

modal.html:

<div class="modal-header"><h3 class="modal-title">我是模态!</h3>

<div class="modal-body"><h1>{{hello}}</h1>

<div class="modal-footer"><button class="btn btn-primary" ng-click="ok()">OK</button><button class="btn btn-warning" ng-click="cancel()">取消</button>

有什么办法可以将 modalCtrl 放在另一个 JavaScript 文件中,并以某种方式将控制器 (modalCtrl) 注入 modalTestCtrl 中吗?

BR

解决方案

当然可以.首先,您应该使用函数声明.

//modalCtrl.js//该文件必须在 modalTestCtrl 控制器之前加载功能 modalCtrl ($scope) {$scope.hello = '作品';};

然后,将 modalTestCtrl 更改为:

app.controller('modalTestCtrl', ['$scope', '$modal', function($scope, $modal) {$scope.openModal = function() {var modalInstance = $modal.open({templateUrl: 'modal.html',controller: modalCtrl,//这个必须是引用,不是字符串尺寸:'sm'});}}]);

通过上述更改,您的代码必须工作.

I am trying to make a modal which can be instantiated from multiple places in the app. from the example given here: Angular directives for Bootstrap the modal controller is in the same file as the controller who instantiates the modal. I want to separate the modal controller from the "app" controller.

index.html:

<!DOCTYPE html>
<html ng-app="modalTest">

  <head>
    <link rel="stylesheet" type="text/css" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css">
  </head>

  <body ng-controller="modalTestCtrl">
    <button ng-click="openModal()">Modal</button>

    <script type="text/javascript" src="http://cdnjs.cloudflare.com/ajax/libs/angular.js/1.2.20/angular.min.js"></script>
    <script type="text/javascript" src="http://cdnjs.cloudflare.com/ajax/libs/angular-ui-bootstrap/0.10.0/ui-bootstrap.min.js"></script>
    <script type="text/javascript" src="http://cdnjs.cloudflare.com/ajax/libs/angular-ui-bootstrap/0.10.0/ui-bootstrap-tpls.min.js"></script>
    <script type="text/javascript" src="modalTest.js"></script>
  </body>

</html>

The controller:

var app = angular.module('modalTest', ['ui.bootstrap']);

app.controller('modalTestCtrl', ['$scope', '$modal', function($scope, $modal) {
    $scope.openModal = function() {
        var modalInstance = $modal.open({
            templateUrl: 'modal.html',
            controller: 'modalCtrl',
            size: 'sm'

        });
    }
 }]);
// I want this in another JavaScript file... 
app.controller('modalCtrl', ['$scope', function($scope) {
    $scope.hello = 'Works';
}]);

modal.html:

<div ng-controller="modalCtrl">
    <div class="modal-header">
        <h3 class="modal-title">I'm a modal!</h3>
    </div>
    <div class="modal-body">
        <h1>{{hello}}</h1>
    </div>
    <div class="modal-footer">
        <button class="btn btn-primary" ng-click="ok()">OK</button>
        <button class="btn btn-warning" ng-click="cancel()">Cancel</button>
    </div>
</div>

Is there any way to put the modalCtrl in another JavaScript file and somehow inject the controller (modalCtrl) into the modalTestCtrl ?

BR

解决方案

Of course, you can do it. First of all you should use a function declaration.

// modalCtrl.js
// This file has to load before modalTestCtrl controller
function modalCtrl ($scope) {
   $scope.hello = 'Works';
};

Then, change the modalTestCtrl like:

app.controller('modalTestCtrl', ['$scope', '$modal', function($scope, $modal) {
$scope.openModal = function() {
    var modalInstance = $modal.open({
        templateUrl: 'modal.html',
        controller: modalCtrl, //This must be a referance, not a string
        size: 'sm'

    });
}
}]);

With the changes above, you code has to work.

这篇关于在单独的 js 文件中带有控制器的 Angular ui 模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
前端开发最新文章
热门教程
热门工具
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆