Working with angularJS in master page asp.net

8

I've been involved with angularJS for these days, so at this stage of learning, a question arose.

How do I work with angularJS in ASP.NET pages that have a master page?

For example, I noticed that the "ng-app" directive should be placed in the html tag of the page. But this tag basically exists on the master page. If I use for example: ng-app="Pessoa.js" , how could it on another child page, use for example, ng-app="Produto.js" ?

I know this must be pretty basic and I may be confusing things, but I already have a legacy asp.net web-forms project, and I will not be able to change it to MVC to do it "ideal" . I'm working with Web Api and AngularJS in this project that already exists (just to make it easier to understand).

    
asked by anonymous 21.08.2014 / 13:38

1 answer

2

A very practical example on the subject does not really change much.

1 - MasterPage.aspx

<%@ Master Language="C#" AutoEventWireup="true" CodeBehind="SiteMasterPage.master.cs" Inherits="WebApplicationForms.SiteMasterPage" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" ng-app="app">
<head runat="server">
    <title></title>
    <script src="Scripts/jquery-1.10.2.js"></script>
    <script src="Scripts/angular.min.js"></script>    
    <asp:ContentPlaceHolder ID="head" runat="server">
    </asp:ContentPlaceHolder>
</head>
<body>
    <form id="form1" runat="server">
    <div ng-controller="ctrl">
        <asp:ContentPlaceHolder ID="ContentPlaceHolder1" runat="server">        
        </asp:ContentPlaceHolder>
    </div>
    </form>
</body>
</html>

In MasterPage , we have the ng-app="app" directives and a div ng-controller="ctrl" , where we have in head a ContentPlaceHolder where in the pages that load MasterPage there can be manipulation example:

2 - Angular.Aspx

<%@ Page Title="" Language="C#" MasterPageFile="~/SiteMasterPage.Master" AutoEventWireup="true" CodeBehind="Angular.aspx.cs" Inherits="WebApplicationForms.Angular" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server">
    <script type="text/javascript">
        angular.module('app', [])
               .controller('ctrl', function ($scope)
               {
                   $scope.itens = [
                        { "Numero": 1, "Nome": "AAAAAA" },
                        { "Numero": 2, "Nome": "BBBBBB" }
                   ];
               });
    </script>
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
    <ul>
        <li ng-repeat="item in itens">{{item.Nome}}</li>
    </ul>
</asp:Content>

Result when rotating the page with the name of Angular.aspx

Surelythisitemhasalogicforittowork,but,italldependsonthewayyouaregoingtomountit.

3-Onlypoliciesondaughters:

<%@PageTitle="" Language="C#" MasterPageFile="~/SiteMasterPage.Master" AutoEventWireup="true" CodeBehind="Angular.aspx.cs" Inherits="WebApplicationForms.Angular" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server">
    <script type="text/javascript">
        angular.module('app', [])
               .controller('ctrl', function ($scope)
               {
                   $scope.itens = [
                        { "Numero": 1, "Nome": "AAAAAA" },
                        { "Numero": 2, "Nome": "BBBBBB" }
                   ];
               });
    </script>
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
    <ul ng-app="app" ng-controller="ctrl">
        <li ng-repeat="item in itens">{{item.Nome}}</li>
    </ul>
</asp:Content>

Note that: the call is <ul ng-app="app" ng-controller="ctrl"> as you want, that is, only on the child pages. The only thing that is in MasterPage is <script src="Scripts/angular.min.js"></script> , to make handling easier.

    
21.08.2014 / 15:00