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