javascript - 无法在动态$route AngularUI模式中访问$scope

标签 javascript angularjs angular-ui

我有一个 AngularJS 测试应用程序,它根据链接路径使用 AngularUI 动态加载模式窗口。我的模态窗口已加载,但模板无法访问模态 Controller 中的 $scope 变量,并且我无法从模板触发任何 $scope 函数。

Here's a plunkr

这是 $routeProvider:

.config(function($routeProvider) {
  $routeProvider
    .when('/page/:name', {
        templateUrl : 'modalContainer',
        controller : 'ModalCtrl'
    })
 })

这是 Controller :

.controller('ModalCtrl',['$scope', '$modal', '$route', function($scope, $modal, $route) {

    console.log("hello I am the ModalCtrl") //This console log runs

    var modalInstance = $modal.open({
        templateUrl : 'modal.html',
    });

    $scope.activity = $route.current.pathParams.name;
    console.log($scope.activity) //This console log runs & shows the correct value,
                                 // but the $scope variable is not visible in the template

    //Modal controls
    $scope.close = function () {
        console.log("close!") //This does not run.
        modalInstance.close();
    };

}]);

我想我以某种方式错误地创建了模态窗口的范围,有人能看到这个问题吗?

最佳答案

您需要为模态窗口创建一个 Controller ,以便它可以访问 $scope,因此代码如下所示:

var modalInstance = $modal.open({
    templateUrl : 'modal.html',
    controller: 'ModalCtrl'
});

var ModalCtrl = function ($scope, $modalInstance) {
    //Modal controls
    $scope.close = function () {
      console.log("close!") //This will now run
      modalInstance.close();
    };
}

关于javascript - 无法在动态$route AngularUI模式中访问$scope,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25939920/

相关文章:

javascript - 防止jQuery UI Datepicker(内联类型)选择今天的日期

css - 使用动画从左向右移动面板但保留其中的一部分,有 Angular ,动画

javascript - 如何使用 Angular UI Router 设置默认 subview

javascript - 是否可以在没有 ActiveX 的情况下运行 JS/html 中的批处理文件?

javascript - 将数据理解为 JQuery 选择器

javascript - 动态创建的 Bootstrap 表松散间距调整

javascript - 无法将js加载到MongoDB中

javascript - 如何选择嵌套 ng-repeat 中的所有复选框

javascript - Chrome 中的 Bootstrap 边框不会消失

javascript - 如何使 Angular 掩码适用于各种信用卡?