2016-06-12 8 views
0

Hallo Ich lerne Winkel und ich weiß nicht, wie man PHP-Variable an Winkel-Controller übergeben und dann durch HTML-Select anzuzeigen. Ich denke, ich sollte dies durch eckige http "Post" -Methode tun, aber es funktioniert nicht. Irgendeine Idee? Bitte helfenPHP Variable zu Winkel

Html-Code:

<div id="find" ng-controller="SelectController"> 
     <ol class="nya-bs-select" ng-model="model" disabled="disable"> 
      <li nya-bs-option="option in options"> 
        <a>{{option.name}}</a> 
      </li> 
     </ol> 
     <div>{{model.text}}</div> 
    </div> 

Php Code:

<?php 
    $text1 = get_field('contact1'); 
    $text2 = get_field('contact2'); 
    $text3 = get_field('contact3'); 
    ?> 

Angular Code:

app.controller('SelectController', ['$scope', function($scope, $http) { 

    var request = $http({ 
    url: "../page-contact.php", 
    method: "POST", 
    data: { 
     data1: $text1, 
     data2: $text2, 
     data3: $text3 
    } 
     }).success(function(response) { 
     console.log(response); 
    }); 

$scope.options = [ 
    {text: data1, name: 'Option A'}, 
    {text: data12, name: 'Option B'}, 
    {text: data3, name: 'Option C'} 
]; 

}]); 

Antwort

0

In Ihrem Angular der Controllerdatei,

$scope.var1 = "<?=$text1; ?>"; 
+0

Es tut mir leid, aber ich weiß nicht, was mit diesem weiter zu tun – Kermit

+0

Sie haben PHP-Variable in eckigen $ Bereich jetzt. Was willst du noch ? Sie können die PHP-Variable im Controller von angular übergeben. –

+0

Es funktioniert nicht. Console.log ($ scope.var) zeigt "" – Kermit

0

Sie benötigen diese in Seite php haben:

$object = $params['json']; 
$var = json_encode($object); 
$json = json_decode($var); 

echo $json->{'text1'}; 
echo $json->{'text2'}; 
echo $json->{'text3'}; 

und in Winkelseite

$scope.options = [ 
    {text: data1, name: 'Option A'}, 
    {text: data12, name: 'Option B'}, 
    {text: data3, name: 'Option C'} 
]; 
$http({ url: "../page-contact.php", 
       method: "POST", 
       headers: {'Content-Type': 'application/x-www-form-urlencoded'}, 
       data: $.param({ json: $scope.options }) 
      }).success(function(data, status, headers, config) { 
       console.log(data) //you have data php 
      }).error(function(data, status, headers, config) { 

      });