0

I'm working on html and angular js for test project to learn angular js.

I've developed one custom input control for masking in html (http://teena.aaadev.info/platform/controls/textbox.html), it's working perfectly. Now I want to use that control in another html page, something like this way:

<div my-control="textbox"></div>

OR

<div my-control="textbox.html"></div>

Once the above code run or viewed in web browser, it'll automatically load the textbox control in that div which inherits all the functionalities of that control. This is the basic thing I need. Afterwards I'm planning for passing the attributes or options for that controls to pick up right format of control.

I've used the following code but it's not working and showing a blank page:

<div ng-include="'text.html'"></div>

I heard about Routing & Multiple Views (https://docs.angularjs.org/tutorial/step_07), is this helpful to achieve my requirements??

Thanks!

1
  • Please be sure when you post that you aren't creating new overly broad tags (like the tag "custom"). Thanks! Commented Dec 16, 2015 at 19:34

2 Answers 2

1

If you want to use separate pages, then definitely use the routing & multiple views tutorial.

In Henry's jsbin example you can define your directive as

textboxApp.directive('myTextbox', [function () { 
   return {
      restrict: 'A',
      scope: {mask: '@'},  // add other variables here
      controller: 'textboxController',
      templateUrl: 'textbox.html'
}]);

Where textbox.html looks something like this

<h3>mask="{{mask}}"</h3>
<input id="{{mask}}" type="text"
       ng-focus="focusInNumber($event)" 
       ng-blur="focusOutNumber($event)" 
       ng-keypress="checkNumer($event)" />

Here's more on directives - I found this pretty useful when I was first learning.

Then, some file like index.html will have the angular app setup that is currently in textbox.html.

<div my-textbox mask="none"></div>
<div my-textbox mask="thousand"></div>
<div my-textbox mask="currency"></div>
Sign up to request clarification or add additional context in comments.

7 Comments

Thanks for the answer. I did tried this way but It seems to be it's not applying to my main html file. I've prepared a demo, please check if you can figure it out: teena.aaadev.info/platform/routes/index.html
You have two modules that do not know about each other, which is why the directive cannot find textboxController. Try getting rid of var testApp = angular.module('testApp', []), changing testApp.directive to textboxApp.directive, and changing the order you import your scripts textbox.js, then test.js. Also, change ng-app="textboxApp".
I think it did the trick and very closer to get my goal. But still having issue like, not getting {{mask}} model value and also some errors to get the event target length, event key code etc. Changes uploaded to server to check if you can help me on this, thanks!
{{mask}} is actually my fault, it should be scope: {mask: '@'}. Did not have time to look at the event issue but check Angular's docs for ng-keydown.
Both issue got resolve. For event issue, I forgot to pass myFucntion($event) parameter. now it's working but after that one more issue raise is I'm not able to write anything in textbox. For this issue I guess something need to check with my existing scripts. Thanks for your help. You're genius!
|
1

What you want to do is to create directives (re-usable html components) instead of creating a controller for your page. The directives themselves are portable and that's where your event handler logic should be.

I've created a jsbin stripped down version of your code for you to play with. All you need is to have "my-textbox" attribute in order to inherit the behavior (it doesn't even need to be on .

<input type="text" my-textbox ng-blur="blur($event)">

http://jsbin.com/hibufiwuxi/edit?html,js,output

2 Comments

I did tried this way but It seems to be it's not applying to my main html file. I've prepared a demo, please check if you can figure it out: teena.aaadev.info/platform/routes/index.html
Got issue resolved with the combination of @abeach3 and yours, thanks!

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.