Eduardo
Eduardo

Reputation: 709

asp net user control with angularjs

I'm using angularJS in an asp.net user control (UC), but, unfortunately, when I try to add my UC in my page, the whole page part that uses angular stops working. I tried to use angular reference separate from UC and the page, but no success.

Page Code:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Abc.aspx.cs" Inherits="WebApplication1.Abc" %>
<%@ Register Src="~/filterUC.ascx" TagPrefix="uc1" TagName="filterUC" %>


<!doctype html>
<html>
<head>
</head>
<body>
    <uc1:filterUC runat="server" ID="filterUC" />
    <div>
      <label>Name:</label>
      <input type="text" ng-model="myModel" placeholder="Enter a name here">
      <hr>
      <h1>Hello {{myModel}}!</h1>
    </div>    
</body>
</html>

UC code:

<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="filterUC.ascx.cs" Inherits="WebApplication1.filterUC" %>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.7/angular.min.js"></script>
<div ng-app>
    <label>Name:</label>
    <input type="text" ng-model="yourName" placeholder="Enter a name here">
    <hr>
    <h1>Hello {{yourName}}!</h1>
</div>

How can I accomplish it?

Regards.

Upvotes: 3

Views: 1686

Answers (1)

Fidel Orozco
Fidel Orozco

Reputation: 1066

I made your example work with these changes:

  1. Move the script to invoke AngularJS into the page Abc.aspx.
  2. Set in the <body> tag ng-app="app" and remove it from the div in your User Control.
  3. Add a script to create your Angular app module.
     <%@ Register Src="~/Controls/filterUC.ascx" TagPrefix="uc1" TagName="filterUC" %>
    <!DOCTYPE html>
    <head runat="server">
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <title></title>
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.7/angular.min.js">
    </script>
    <script>
        angular.module('app', []);
    </script>
    </head>
    <body ng-app="app">
    <div>
      <label>Name:</label>
      <input id="input1" type="text" ng-model="myModel" placeholder="Enter a name here">
      <hr>
      <h1>Hello {{myModel}}!</h1>
    </div>
    <uc1:filterUC runat="server" id="filterUC" />
    </body>
    </html>

Upvotes: 1

Related Questions