0

I'm trying to use Angular JS with Laravel 4.2. I've changed the blade's default {{}} into [[]], it works perfectly, now the problem is that when I puts Angular JS code in main layout (view) it works but when I put Angular JS code in a child view(actually child view extends the main layout and section is yielded using @section('contents') ...Angular JS code... @stop) it doesn't work at all, browser can't render the Angular JS exprerssion and shows it as it is in code editor! Here is my "main layout.blade.php" and "child.blade.php":

"main.layout.blade.php"

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>Customer Management</title>

    <!-- Bootstrap -->
    [[ HTML::style('css/bootstrap.min.css') ]]
    [[ HTML::style('css/custom.css') ]]

    [[--angular js files--]]
    [[HTML::script('/js/angular/angular.min.js')]]
    [[HTML::script('/js/controllers.js')]]

    [[--jquery bootsrtap js files--]]
    [[ HTML::script('js/respond.js') ]]
    [[ HTML::script('js/jquery-1.11.2.min.js') ]]
    [[ HTML::script('js/bootstrap.min.js') ]]

</head>

<body>

    @include('layouts.main-menu')

    <div class="container" id="container">
        <div ng-app=""><!-- This Angular JS works fine -->
            <p>Name : <input type="text" ng-model="name"></p>
            <h1>Hello {{name}}</h1>
        </div>

        @yield('content')
    </div>

    @include('layouts.footer')


    <script>
        $("nav ul li").on("click", function() {
            $("nav ul li").removeClass("active");
            $(this).addClass("active");
        });
    </script>
</body>
</html>

Child.blade.php

@extends('layouts.main')
<!doctype html ng-app="myApp">
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>

</head>
<body>

    @section('content')
        <div ng-app=""><!-- This Angular JS doesn't work fine -->
            <p>Name : <input type="text" ng-model="name1"></p>
            <h1>Hello {{name1}} </h1>
        </div>
    @stop
</body>
</html>

You can see clearly that same Angular Code snippet is used in both files but in "layout.main.blade.php" it works and in "child.blade.php" it doesn't work as it is in between @section('content') ... @stop I can't get understand why this is and how to solve it!!!!!!

This is the response from network panel:

GET localhost:8000

200 OK

localhost:8000

3.4 KB

[::1]:8000


141ms
HeadersResponseHTMLCacheCookies

<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>

</head>
<body>

    </body>
</html>
<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>Customer Management</title>

    <!-- Bootstrap -->
    <link media="all" type="text/css" rel="stylesheet" href="http://localhost:8000/css/bootstrap.min
.css">

    <link media="all" type="text/css" rel="stylesheet" href="http://localhost:8000/css/custom.css">



        <script src="http://localhost:8000/js/angular/angular.min.js"></script>

    <script src="http://localhost:8000/js/controllers.js"></script>


        <script src="http://localhost:8000/js/respond.js"></script>

    <script src="http://localhost:8000/js/jquery-1.11.2.min.js"></script>

    <script src="http://localhost:8000/js/bootstrap.min.js"></script>


</head>

<body>

    <div class="row">
    <nav class="navbar navbar-default navbar-fixed-top navbar-inverse">
        <div class="navbar-header">
            <button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#collapse"
>
                <span class="sr-only">Toggle navigation</span>
                <span class="glyphicon glyphicon-arrow-down"></span>
                MENU
            </button>
        </div>
        <div class="collapse navbar-collapse" id="collapse">
            <ul class="nav navbar-nav">
                                                    <li class="active"><a href="/" ><span class="glyphicon
 glyphicon-home"></span> Home</a></li>
                                <!--<li class="dropdown"><a href="#" data-toggle="dropdown">Dropdown
 <span class="caret"></span></a>
                    <ul class="dropdown-menu">
                        <li><a href="#">Dr. Winthrop</a></li>
                        <li><a href="#">Dr. Chase</a></li>
                        <li><a href="#">Dr. Sanders</a></li>
                    </ul>
                </li>-->
                <li><a href="cv/index"><span class="glyphicon glyphicon-edit"></span> Make CV</a></li
>
                <li><a href="#"><span class="glyphicon glyphicon-eye-open"></span> Example</a></li>

                <li><a href="#"><span class="glyphicon glyphicon-info-sign"></span> About</a></li>
            </ul>

            <ul class="nav navbar-nav navbar-right">
                                    <li><a href="http://localhost:8000/users/signin">Sign in</a></li
>
                    <li><a href="http://localhost:8000/users/newaccount">Sign Up</a></li>
                            </ul>

        </div>
    </nav>
</div>


    <div class="container" id="container">
        <div ng-app="">
            <p>Name : <input type="text" ng-model="name"></p>
            <h1>Hello {{name}}</h1>
        </div>

                <div ng-app="">
            <p>Name : <input type="text" ng-model="name1"></p>
            <h1>Hello {{name1}} </h1>
        </div>
        </div>

    <footer class="footer row ">
    <div class="col-lg-4 col-sm-4 col-md-4" id="links">
        <h4>Links</h4>
        <ul class="list-unstyled">
            <li>Link 1</li>
            <li>Link 2</li>
            <li>Link 3</li>
            <li>Link 4</li>
        </ul>
    </div>
</footer>

    <script>
        $("nav ul li").on("click", function() {
            $("nav ul li").removeClass("active");
            $(this).addClass("active");
        });
    </script>
</body>
</html>


1 request

3.4 KB

141ms (onload: 1.06s)
Khuram
  • 1,820
  • 1
  • 26
  • 33
  • Can you show the combined output from the PHP request as well (just check the response in the network panel and post that as well). Would recommend just having the server side act as an API using JSON to communicate and write the client code separately, it will make debugging and expanding or replacing/supplementing parts for the client or server simpler. – shaunhusain May 06 '15 at 04:21
  • Hello @shaunhusain ! I've added response in this question, can you help me here! – Khuram May 06 '15 at 06:28

1 Answers1

0

I'm not a blade user so I can't tell you exactly how to fix this. I do use PHP but I use Eloquent (the ORM part of Laravel) and use Slim for doing the server side routing. I believe it's best to write your HTML simply as HTML, use $http (basically the same as $.ajax) to get your data into the client code but write them as totally separate code bases. This will afford you a lot more flexibility and greatly simplify the task of debugging since you aren't trying to figure out issues between the server and client simultaneously. You can test the server in isolation using curl or POSTMan and then can separately build/test the client using mock objects or real requests to your server side component.

On the client side of things what you show being output has quite a few problems though, multiple html tags (should be 1 for the document) multiple ng-app (same, 1 for the page typically on the html or body tag).

For bootstrap use ui-bootstrap, you need the regular bootstrap.css file and the ui-bootstrap-tpls.js file but not bootstrap.js.

Where you have the jQuery to bind to a click event on some elements you should be using an ng-click directive in the markup instead, also can use ng-class to conditionally add/remove a class to an element based on the state of a model.

It seems like you have an understanding of roughly how to use all these things but you need to clearly separate things and stop using jQuery for a while to get used to how to do things with Angular properly. If you try to use jQuery outside of directives or tests with Angular you're typically doing something wrong. For details see "Thinking in AngularJS" if I have a jQuery background?

Community
  • 1
  • 1
shaunhusain
  • 19,630
  • 4
  • 38
  • 51