【问题标题】:AngularJS Php file with angular in it not working after $httpAngularJS Php 文件在 $http 之后不起作用
【发布时间】:2014-10-21 06:59:02
【问题描述】:

我有一个 php 文件,其中包含一堆有角度的东西,当我直接包含它时它工作正常,但是一旦我通过 $http 包含该 php 文件,如下所示:

$http.get('include/step'+step+'.php').
success(function(data, status, headers, config) {
    $("#tab_"+step).html(data);
});

它停止工作,然后我可以看到以下内容:{{category.title}} 等显示为文本而不是获取其值。

我从 Angular 开始,我试图让 Angular 识别通过 ajax 响应或 $http 响应添加的代码。

我也试过了

但我得到了这个 angular.bootstrap(data);

Error: [ng:areq] Argument 'eventCatCtrl' is not a function, got undefined

eventCatCtrl 是我在通过 $http 生成的文件中的第一个控制器。

我也做了这个 angular.bootstrap(data, ['eventApp']); eventApp 是我的主模块的名称。 仅供参考,所有这些 http 的东西都在一个 div 中完成,如果重要的话,我已经在 body 上定义了我的模块。

应更多代码的要求:

函数由此触发:

<div class="form-group progress-tracker__buttons" ng-controller="StepChange">
    <input type="button" class="btn btn-primary btn-save" value="Save">
    <input type="button" class="btn btn-default btn-next disabled" value="Next" ng-click="changeStep($event)">
    <div class="last-saved">Last saved: Not saved yet</div>
</div>

这是我的模块、控制器和函数:

var eventApp = angular.module('eventApp', []);

eventApp.controller('StepChange', function($scope, $http){
    $scope.changeStep = function($event){
        $http.get('include/step2.php').
        success(function(data, status, headers, config) {
            $("#tab_2").html(data);
        });
    };
});

在我的 step2.php 文件中,我有这部分:

<div class="panel-group form-group sortable" id="catAccordion" ng-controller="eventCatCtrl">
    <div ng-repeat="category in categories.list">
        <p>{{category.title}}</p>
    </div>
</div>

我有这样的工厂:

eventApp.factory('EventCategories', function () {
    var EventCategories = {};
    EventCategories.list = [
        {
            name: "Robert Downey Jr.",
            character: "Tony Stark / Iron Man"
        },
        {
            name: "Chris Evans",
            character: "Steve Rogers / Captain America"
        }
    ];
    return EventCategories;
});

我有一个这样的控制器用于那个工厂:

eventApp.controller("eventCatCtrl", function($scope, EventCategories){
    $scope.categories = EventCategories;
});

【问题讨论】:

  • 请提供更多代码。
  • @cbass 请看一下,如果您需要其他任何东西,请告诉我。
  • 您需要 $http.get() 一个实际的 Web 端点,而不是包含。角度无法处理 php 文件。

标签: php ajax angularjs angularjs-scope angularjs-ng-repeat


【解决方案1】:

在 Angular 启动后,您无法初始化控制器。因此,您收到错误

Error: [ng:areq] Argument 'eventCatCtrl' is not a function, got undefined

另外,$http.get 应该用于保存/获取数据。要包含其他布局和 UI,您应该使用模板和指令。您将能够使用 $scope 中的变量来控制嵌入 html 内容的显示。

Angular 中的延迟加载

http://ify.io/lazy-loading-in-angularjs/

此外,您还必须公开控制器注册服务,如下所示:

eventApp.config(['$controllerProvider', function($controllerProvider) {

    eventApp.controllerProvider = $controllerProvider;
}]);

并且,在您注册控制器的地方,使用以下内容:

eventApp.controllerProvider("eventCatCtrl", function($scope, EventCategories){
    $scope.categories = EventCategories;
 });

【讨论】:

  • 我明白了。我以前使用 ajax 来获取文件,然后想到实现 angular 而不是其他东西,我将尝试使用模板。但是有没有办法通过 $http 调用文件来使文件工作,现在我知道这不是最理想的情况,我可能会避免使用它,但有没有办法让它工作?
  • 有一种使用 AJAX 加载文件的方法,因为您必须通过路由,必须在引导后设置一种调用控制器的方法。如果你有兴趣,我会发布一些相同的文章...
  • 你能告诉我吗,我之前使用 AJAX 加载文件,但 angular 无法识别它们,并且显示的结果与我现在相同。
  • 谢谢 Rohit,我会检查一下。
  • 我应该使用 ng-include、ng-if + ng-show 还是 ajax 延迟加载?就性能而言,最好的方法是什么,我只想在单击按钮后在 div 中添加一个模板。
猜你喜欢
  • 1970-01-01
  • 2017-12-19
  • 2015-09-27
  • 1970-01-01
  • 2012-07-15
  • 2016-11-10
  • 1970-01-01
  • 1970-01-01
  • 2017-12-22
相关资源
最近更新 更多