简体   繁体   English

Angular + Laravel:通过ngRoute加载视图

[英]Angular + Laravel : loading view via ngRoute

I'm facing this problem when combining Laravel and Angular: 当组合Laravel和Angular时,我面临着这个问题:

Following a very simple tutorial I found online, I was trying to load a view using the ng-view directive. 在网上找到了一个非常简单的教程之后,我试图使用ng-view指令加载ng-view However, I am unable to actually load the template. 但是,我无法实际加载模板。 This is my app.js code: 这是我的app.js代码:

(function() {

    var app = angular.module('profuApp', ['ngRoute']);

    app.config(['$routeProvider', '$locationProvider', function($routeProvider, $locationProvider) {
        $routeProvider
            .when('/inicio', {
                templateUrl: '../templates/inicio.html',
                controller: 'homeCtrl'
            })
            .otherwise({ redirectTo: '/' });
    }]);

    app.controller('homeCtrl', ['$scope', '$http', function($scope, $http) {
        $http.get('http://localhost:8888/profucom/public/getData').success(function(data) {
            $scope.datos = data;
        });
    }])

})();

My file tree: 我的文件树:

app/
bootsrap/
public/
   js/
      angular.min.js
      angular-route.min.js
      app.js
   templates/
      inicio.html
. . .

The site loads normally, but when I watch the source code, instead of watching the template inside of inicio.html , this happens... 该站点正常加载,但是当我观看源代码时,而不是观看inicio.html的模板,这发生了……

Code with within my index.blade.php file 我的index.blade.php文件中的代码

<div ng-view></div>

Code I see in the browswer's source code 我在浏览器的源代码中看到的代码

<!-- ng-view: -->

The network tab on Chrome does not show a 404 error trying to load the view. Chrome上的network标签未显示404错误,试图加载视图。

What I've tried so far: 到目前为止,我已经尝试过:

  1. Placing templates inside the js folder (templateUrl: 'templates/inicio.html') 将模板放在js文件夹中(templateUrl:'templates / inicio.html')
  2. Placing templates inside the public folder (templateUrl: '../templates/inicio.html') 配售模板内public文件夹(templateUrl:“../templates/inicio.html”)
    • also, templateUrl: '/public/templates/inicio.html' 还有templateUrl: '/public/templates/inicio.html'
  3. Placing templates inside the root folder (templateUrl: '/templates/inicio.html') 将模板放在根文件夹中(templateUrl:'/templates/inicio.html')
  4. Placing just the file inside the js folder: (templateUrl: 'inicio.html') 仅将文件放在js文件夹中:(templateUrl:'inicio.html')

None of the above seem to work. 以上似乎都不起作用。 Any help, please? 有什么帮助吗?

EDIT I also see this happenning in the url: instead of myapp/public/inicio it loads like myapp/public/inicio#/ 编辑我也在URL中看到了这种情况:代替myapp/public/inicio它加载为myapp/public/inicio#/

Where is your index.blade.php located? 您的index.blade.php在哪里? templateUrl should be the path to the template from whatever file loads the app.js code (I'm assuming that's index.blade.php). templateUrl应该是从任何文件加载app.js代码到模板的路径(我假设那是index.blade.php)。 So if that's in the root directory, templateUrl: '/public/templates/inicio.html' 因此,如果在根目录中,则templateUrl: '/public/templates/inicio.html'

As for the issue with the # , read this somewhat related question 至于#的问题,请阅读与此相关的问题

Once again, it was nothing but a human mistake. 再一次,这不过是人为的错误。 I'm sorry and thankful at the same time for those who spent time trying to answer and comprehend my problem. 对于那些花时间试图回答和理解我的问题的人,我同时感到遗憾和感谢。 This is the solution: 这是解决方案:

I was using the latest stable version directly from a CDN, version 1.3.5 ; 我直接使用CDN的最新稳定版本1.3.5 however - and just to see what'd happen - I changed to version 1.2.28 . 但是-只是为了看看会发生什么-我更改为1.2.28版。 What's the difference? 有什么不同? A little syntax. 一点语法。

Instead of what I did above... 而不是我上面做的...

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

I did this: 我这样做:

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

Changing from one version to another was the answer, and changing a little the syntax. 从一个版本更改为另一个版本就是答案,并且只需更改一点语法即可。

I hope this helps anyone as distracted as I was. 我希望这能像我一样帮助任何分心的人。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM