本文介绍了在PartialView angularjs控制器不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个观点,其中包含一个链接调用PartialView。

I have a View which contains a link to call a 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一个函数来显示它。

My HttpService service has a function that calls an action from the controller and returns a PartialView in order to show it.

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

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

The PartialView is showing perfectly. The problem occurs when I try to add a controller to that PartialView content.

<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>

控制器只是不能按预期工作。无我把控制器内部出现在上面核实。发生了什么?谢谢大家!

The controller just don't work as expected. None I put inside the controller appears in that div above. What's happening? Thank you all!

推荐答案

停止使用jQuery ...

Stop using jQuery...

问题是, $('模式。)HTML(结果); 只添加HTML的东西用 .modal 类。你需要做的是用AngularJS编译模板,是这样的:

The problem is that $('.modal').html(result); is only adding the HTML to something with a .modal class. What you need to do is to compile the template using AngularJS, something like:

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();
                    };
                });
        }
    };
});

工作实例

这篇关于在PartialView angularjs控制器不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-22 10:53