如何重新初始化angularjs应用程序

时间:2012-08-18 22:51:27

标签: soap soap-client angularjs

这是我的代码:

function MainCtrl($scope) {
    $scope.thisarray = globalObj;

    $scope.loadthis = function (index) {
        return thisarray[index];
    }
}

这是标记:

<table ng-controller="MainCtrl">
    <tr>
        <td ng-repeat="element in thisarray">
            {{loadthis($index)}}
        </td>
    </tr>
</table>

我正在通过SOAP回调加载一些数据:

    SOAPCLient.invoke(url, methodName, pl, true, function(obj) {globalObj = obj;

    **angular.bootstrap("html");**//here is where I try to reinitialize the app 

});

如果您查看SOAP来电,我会使用bootstrap重新初始化该应用,但它一直没有运作。 loadthis不会在HTML表格中显示任何数据。谁能在这帮助我?

2 个答案:

答案 0 :(得分:1)

每次从外部源获取数据时,都不应重新初始化AngularJS应用程序。而不是破坏和重新创建AngularJS的方法是在范围对象上使用$ apply方法。

伪代码(在你的控制器中)就像:

SOAPCLient.invoke(url, methodName, pl, true, function(obj) {
    $scope.apply(function(){
        $scope.thisarray = obj;
    });
});

此外,如果我正确理解了您的代码,那么当您的SOAP调用为所有对象带回数据时,您将为转发器中的每个项目(ng-repeat)调用SOAP调用。因此,更好的方法是进行SOAP调用(如上面的代码片段所示),将数据分配给范围中的变量,让ngRepeat发挥其魔力。

我可能误解了你要在这里实现的目标,所以在你的代码中使用jsFiddle / plunker将有助于提供更详细的响应。

答案 1 :(得分:1)

您可以在尝试强制推送时使用$ .watch然后使用$ .apply来监听SOAP的更改。请参阅如何使用here

的示例