3

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.

1 Answer 1

1

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>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.