【问题标题】:angularjs controller in PartialView not workingPartialView中的angularjs控制器不起作用
【发布时间】:2014-07-30 17:41:03
【问题描述】:

我有一个视图,其中包含一个调用 PartialView 的链接。

<div data-ng-controller="MainController">
    <a href="#" data-ng-click=callPartialView()">
        Click here to see the details.
    </a>
</div>

<script>
    app.controller('MainController', ['$scope', 'HttpService', 
        function($scope, HttpService) {

        $scope.callPartialView = function() {
            HttpService.getModal('/Controller/ShowModalMethod', {});
        };
    }]);
</script>

我的 HttpService 服务有一个函数,它从控制器调用一个动作并返回一个 PartialView 以显示它。

getModal = function(url, params) {
    $http.get(url, params).then(function(result) {
        $('.modal').html(result);
    });
}

PartialView 显示完美。当我尝试将控制器添加到该 PartialView 内容时,就会出现问题。

<div class="wrapper" data-ng-controller="PartialViewController">
    <span data-ng-bind="description"></span>
</div>

<script>
    alert('This alert is shown.');
    app.controller('PartialViewController', ['$scope', 'HttpService', 
        function($scope, HttpService) {

        $scope.description = "That's the content must have to appear in that bind above, but it isn't working properly.";
    }]);
</script>

控制器无法按预期工作。我放在控制器内的没有出现在上面的那个 div 中。发生了什么?谢谢大家!

【问题讨论】:

    标签: angularjs asp.net-mvc-partialview angularjs-controller


    【解决方案1】:

    停止使用 jQuery...

    问题在于 $('.modal').html(result); 只是将 HTML 添加到具有 .modal 类的内容中。您需要做的是使用 AngularJS 编译模板,例如:

    app.factory('HttpService', function($document, $compile, $rootScope, $templateCache, $http) {
    
        var body = $document.find('body');
    
        return {
            getModal: function (url, data) {
    
                // A new scope for the modal using the passed data
                var scope = $rootScope.$new();
                angular.extend(scope, data);
    
                // Caching the template for future calls
                var template = $http.get(url, {cache: $templateCache})
                    .then(function (response) {
    
                        // Wrapping the template with some extra markup
                        var modal = angular.element([
                            '<div class="modal">',
                            '<div class="bg"></div>',
                            '<div class="win">',
                            '<a href="#" class="icon cross"></a>',
                            '<div>' + response.data + '</div>',
                            '</div>',
                            '</div>'
                        ].join(''));
    
                        // The important part
                        $compile(modal)(scope);
                        // Adding the modal to the body
                        body.append(modal);
    
                        // A close method
                        scope.close = function () {
    
                            modal.remove();
                            scope.destroy();
                        };
                    });
            }
        };
    });
    

    工作示例

    http://jsfiddle.net/coma/6j66U/

    【讨论】:

    • 谢谢,我会看看这段代码,然后回来告诉你结果!
    猜你喜欢
    • 2019-08-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-11-03
    • 2017-04-19
    • 1970-01-01
    • 2023-03-24
    相关资源
    最近更新 更多