Angularjs - directives do not work via ajax

Asked

Viewed 85 times

0

I have a page: index.php and the app.js. in my app I make a load for main.php which is the initial loading of the site. within the main he will bring select, div with ng-repeat, ng-source, etc..

But when the content of the main was part of the index it worked normal, but now it just doesn’t work anymore it loads the page, plus the directives: ng-repeat, ng-source, etc seem not to work. I’ll put an excerpt of the code for you to see

app js.

var app = angular.module("MeuApp", []);
app.controller("MeuAppCtrl", function($scope, $http) {
$scope.msg = "Olá Usuários";
var carregarMain = function() {
    $http.get("http://localhost/main.php").success(function(data, status) {

        $("#mainPage").html(data);
        carregarBanner();
    });
};
var carregarBanner = function() {
    $http.get("http://localhost/banner.php").success(function(data, status) 

{
            $scope.banners = data;
            alert(data[0].foto);
        });
    };
    carregarMain();
});

index php.

<div id="mainPage"></div>

main.php

<div ng-repeat="banner in banners">
  {{banner.foto}}
  <div class="item">
    <img ng-src="{{banner.foto}}" style="height: 600px;" alt="">
  </div>
</div>
  • 1

    puts the full index file to view the Imports

  • 1

    1 - Put the most complete codes of index and main. 2 - Why are you using php when you are already using angular and jquery? One that in your code has nothing of php being used, another that if you are using angular, have no need to use neither jquery and much less php, the less frameworks you load, the better, both in question of data usage size, how much on the matter of loading time and integrity of your code (helps to have less bizarre error by conflicts between frameworks)

1 answer

1


Here is what happened:

i was doing everything else, everything else wrong! I was calling the main.php file via http.get().

What I have done: 1 - I transformed the . php files into . html as indicated, leaving only the files that would return the json to me as . php 2 - instead of using $http.get(), I started using ngRoute and ngView. 3 - I made the appropriate corrections to the Controller through such changes.

Okay, everything was pretty, working as wanted. Thanks guys for the effort, you guys once again helped me!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.