将Laravel与Angular JS一起使用时出现“未定义的常数/变量"错误……无法在刀片中显示变量 [英] “Undefined Constant/Variable” Error using Laravel with Angular JS…Unable to show variable in blade

查看:129
本文介绍了将Laravel与Angular JS一起使用时出现“未定义的常数/变量"错误……无法在刀片中显示变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用laravel和angular js构建一个通用的后端办公室消息传递应用程序,我在仪表板控制器(扩展了基本控制器)中有一个变量(名为receiveved)(显示未读新消息的总数),定义为如下:

I am building a common backend office messaging app using laravel and angular js, I have a variable(named recieved) (which shows the total number of unread new messages) in my dashboard controller (which extends the base controller) defined as follows:

仪表板控制器:

var $data = array();

public function dashboardData(){
$toReturn = array();
$toReturn['siteTitle'] = $this->data['panelInit']->settingsArray['siteTitle'];


//------other things-----------//


$toReturn['stats'] = array();

//------some other things-----------//


$toReturn['stats']['recieved'] = messagesList::where('userId',$this->data['users']->id)->where('messageStatus',1)->count();

}
return json_encode($toReturn);

}

我正在使用angular to进行数据/变量传递/传递...以下是我的angular代码快照:-

I am using angular to for data/variable transfer/passing...following is my angular code snapshots:-

Routes.js

skyla.config(function($routeProvider,$locationProvider) {

$routeProvider.when('/', {
  templateUrl : 'templates/main.html',
  controller  : 'dashboardController'
})

app.js

skyla.controller('mainController',function(dataFactory,$rootScope,$route,$scope) {
  var data = $rootScope.dashboardData;
  $scope.phrase = $rootScope.phrase;

  $scope.dashboardData = data;
  $rootScope.phrase = data.language;
  $scope.phrase = data.language;


}

通过使用角度{{}}语法,变量(已接收)显示在我的Laravel的main(index/default/home/无论您如何称呼)html模板中:

The variable (recieved) is shown in my main(index/default/home/whatever you call it) html template of Laravel by using the angular {{}} syntax as follows:

HTML(用于Main.html模板)

  <-------------some things-------------------------->

  <div class="counter">
  <div class="informational">{{dashboardData.stats.recieved}}</div>
  </div>

  <-------------some other things-------------------------->

它成功显示了输出....

and it successfully shows the output....

但是我想在我用于基本布局目的的layout.blade.php中显示相同的数据,以便接收到的新消息的数量始终出现在每个用户的导航栏标题中...

but I want to show this same data in a layout.blade.php that I am using for basic layout purposes so that the number of received new messages always appear in the navbar header for each user...

我在layout.blade.php(目录:/app/views)中使用相同的html结构,语法为{{dashboardData.stats.recieved}},语法为:

I am using the same html structure in the layout.blade.php (directory:/app/views) with angular {{dashboardData.stats.recieved}} syntax as:

HTML(用于布局刀片)

  <body ng-app="skyla" ng-controller="mainController">

  <-------------some things-------------------------->

  <div class="nav">
  <div class="informational">{{dashboardData.stats.recieved}}</div>
  </div>

  <-------------some other things-------------------------->

  </body>

但是我收到未定义的常量:dashboardData"和未定义的变量"错误...

but I am getting "Undefined Constant:dashboardData" and "undefined Variable" errors...

以下是我的Laravel route.php的一瞥:-

following is a glimpse of my Laravel routes.php:-

routes.php

Route::group(array('prefix'=>'/','before'=>'auth.Ui|auth.token|api.csrf'),function(){
    Route::get('/','DashboardController@index');

    Route::get('/dashboard','DashboardController@dashboardData');
    Route::get('/dashboard/baseUser','DashboardController@baseUser');

    //-------------some things-------------//

Route::post('/dashaboard','DashboardController@dashaboardData');

我在laravel上与angular一起是相当陌生的,我不确定是否将变量值通过php或angular传递给布局秃头,这将是安全可行的做法...

I am quite new on laravel altogether with angular, I am not sure whether to pass the variable value to layout balde with php or angular and which would be the secure and a feasible practise...

我偶然发现了"view :: share"和"view:make"(我尝试过:return view('layout',['dashboardData'=> $ toReturn]);)事情,但并不太得到它如何完美地做到&安全地没有一个错误...

I have stumbled upon the "view::share" and "view:make" (I have tried:return view('layout', ['dashboardData' => $toReturn]);) thing but didn't quite get it how to do it perfectly & securely without a single error...

"/app/libraries/initiation.php"中也有一个库设置,该库设置将一些数据反映到layout.blade.php ...

I also have a library setup in the "/app/libraries/initiation.php" that reflects some data to layout.blade.php...

我的问题

在最简单但正确的实践中我该怎么做,这样我就可以在layout.blade.php中使用有角的{{dashboardData.stats.recieved}}语法,而不会出现错误"Undefined Constant"和"undefined Variable". .. ??? 请解释一下,告诉我是否需要更多信息...我正在接一个前开发人员未完成工作的地方进行这项工作...

What would I do in the most easiest yet correct practise so that I can use angular {{dashboardData.stats.recieved}} syntax in the layout.blade.php without the errors "Undefined Constant" and "undefined Variable"...??? Please explain, tell me if you need more info...I'm picking up this work inbetween where a former dev has left the work uncompleted...

推荐答案

在我看来,由于两者都使用了{{}}括号,因此您在刀刃和角度之间存在冲突.角度也有其自己的渲染视图的方式. layout.blade.php将{{}}解释为自己的名称,而不是角度.在纯" html中,您不会遇到此问题,因为它是在用户端通过角度呈现的.

It seems to me that you have a conflict between blade and angular since {{ }} parentheses are used in both. Also angular has its own way of rendering views. The layout.blade.php interprets {{}} as its own instead of angulars. And in "pure" html you don't have that problem because it is rendered by angular on users side.

(我认为)最简单的方法是将Laravel和Angular文件分开,并将Laravel用作angular应用程序的API.因此,如果您打算在一段时间内进行开发,则可以将刀片文件转换为html文件,以防止将来出现麻烦. :)

The easyest way to do what you're asking for (in my opinion) is to keep Laravel and Angular files separated and use Laravel as an API for angular app. So if you're planing on developing this for some time I would transform blade files into html files for preventing future headaches. :)

解决这种情况的一种方法是用blade作为字符串回显此行,或者用&#123(大括号的html代码)交换大括号. 所以基本上:

That said a way of hacking this situation is to echo this line with blade as a string or to swap curly braces with &#123 (html code for curly brace). So basically:

<div class="informational">&#123;&#123 dashboardData.stats.recieved  &#123;&#123 </div>

这篇关于将Laravel与Angular JS一起使用时出现“未定义的常数/变量"错误……无法在刀片中显示变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆