$ http请求后加载控制器

问题描述 投票:0回答:2

我正在使用这样的$ http请求:

$http({   
    method: 'POST', 
    url: url,
    data : {'some':'data'},
}).
success(function(data, status, headers, config) {
     container.css({    opacity : '0.0' }).html(  data  ).delay(50).animate({   opacity : '1.0' }, 300);
})

$ http响应开始如下:<div class="tree" ng-controller="systemDataTree"><ul><li>一切正在加载它应该,我不想使用路由。正在加载的内容中将包含ng-controller="myController",但加载后不会执行。

如何让它执行加载HTML响应的控制器?我不想在$http.success中执行控制器,因为会使用许多不同的控制器

ajax angularjs controller
2个回答
0
投票

DONE ::

这是解决方案:

success(function(data, status, headers, config) {
     container.css({    opacity : '0.0' }).html(  data  ).delay(50).animate({   opacity : '1.0' }, 300);
})

而不是附加纯data使用$compile(data)($scope),你可以在里面添加任意数量的控制器。像这样你可以自定义加载你的东西并使用你想要的控制器


0
投票

如果错误请纠正我:你想在响应来自网络服务后运行第二个控制器吗?正确?

为此,您可以将模板移动到ng-template中

<script type="text/ng-template" id="innerTemplate">
   <div ng-controller="myFirstCtrl">
</div>

</script>

在你的主视图中你可以有这样的

<div ng-controller="controllerWhichCallsHttp">
 your html here

 <div ng-include="variable"></div>
</div>

controllerWhichCallsHttp将具有以下$ http

$http({   
method: 'POST', 
url: url,
data : {'some':'data'},

}).success(function(data, status, headers, config) {

 $scope.variable="innerTemplate";  //i.e. after you get the response set the value of the variable with the id of the template and then myFirstCtrl will execute
 container.css({    opacity : '0.0' }).html(  data  ).delay(50).animate({   opacity :    '1.0' }, 300);
})
© www.soinside.com 2019 - 2024. All rights reserved.