【问题标题】:How to render angular template in ajax如何在ajax中渲染角度模板
【发布时间】:2017-10-25 17:13:44
【问题描述】:

我在有角度的页面上。 第 1 页: 在哪个配置中

.when('/Test/:empId', {
        templateUrl:'/Templates/test.html',
        controller: 'TestController'
    })

.when('/Test/:depId', {
            templateUrl:'/Templates/test1.html',
            controller: 'Test1Controller'
        })

然后我有另一个页面

第 2 页: 此页面是带有按钮的普通 mvc razor 页面

Button1

单击该按钮时,我想显示包含的模式 第1页

所以我写了

                $.ajax({
                        url: '/Templates/test.html',
                        type: 'GET',
                        success: function (responce) {
                            var elem = angular.element(responce);
                            $('#testModalBody').html(elem);
                            $('#testModal').modal('show');
                            //console.log('test responce');
                        }
                    });

我只是得到 html 响应,它没有执行 'TestController' 所以范围变量没有得到在 TestController 中声明的值,并且无法看到数据 html 页面。只看静态 html 模板。

【问题讨论】:

  • 尝试使用$compile(angular.element()($scope))
  • @JayantPatil : 获取 angular.element(...) 不是函数错误

标签: angularjs asp.net-mvc angularjs-directive angular-ui-router


【解决方案1】:

您应该使用 Angular JS 的 $http 服务,而不是使用 $.ajax。

你可以使用 ng-bind-html,而不是像这样 $('#testModalBody')。

要绑定 HTML,请阅读以下链接。

https://www.w3schools.com/angular/ng_ng-bind-html.asp

【讨论】:

  • 我想让'testBody'独立于角度。我想使用像局部视图这样的模板。
  • 贴出你完整的js代码。你在哪里写了 $ ajax 代码在你的文件中?
猜你喜欢
  • 2018-11-25
  • 2012-01-11
  • 2023-03-11
  • 1970-01-01
  • 1970-01-01
  • 2016-11-13
  • 2020-11-17
  • 2022-09-27
  • 1970-01-01
相关资源
最近更新 更多