angularjs-directive

Components and directives in angular 1.5

只谈情不闲聊 提交于 2019-12-03 03:20:56
The big feature changes in Angular 1.5 are surrounding the support of components . component('myComponent', { template: '<h1>Hello {{ $ctrl.getFullName() }}</h1>', bindings: { firstName: '<', lastName: '<' }, controller: function() { this.getFullName = function() { return this.firstName + ' ' + this.lastName; }; } }); While this is all good, I am not sure how this differs from directives. What are the benefits of using components over traditional custom directives? And are components in Angular 1.5 and Angular 2 the same? The .component is now preferred way of writing code because it favors

Image upload directive (angularJs and django rest framework)

非 Y 不嫁゛ 提交于 2019-12-03 03:07:41
I need an image upload directive, here is how my code looks like: # Model class transporter(models.Model): company_name = models.CharField(max_length=100) address = models.CharField(max_length=100) image = models.FileField(upload_to=upload_path,blank=True, null=True) def upload_path(self, filename): return 'photos/%s/%s' % (self.company_name, filename) # Serializer class transporterSerializer (serializers.HyperlinkedModelSerializer): username = serializers.Field(source='username.username') class Meta: model = transporter fields = ('id','company_name','address','image') it works with only

Breaking a form between multiple tabs in angular breaks validation

亡梦爱人 提交于 2019-12-03 02:58:28
I have an angular form spitted between several tabs with angular UI directive. <form name="campaignForm" class="form-horizontal" novalidate > <input type="text" name="title" class="input-xxlarge" placeholder="Campaign title" ng-model="campaign.title" required> <span ng-show="campaignForm.title.$error.required" class="help-inline"> Required</span> <tabset> <tab> </tab> <input type="email" name="emailFrom" class="input-xsmall" placeholder="From email address" ng-model="campaign.info.emailFrom" required="" /> <span ng-show="campaignForm.emailFrom.$error.required" class="help-inline"> Required<

ng-repeat and ng-controller on the same DOM element

左心房为你撑大大i 提交于 2019-12-03 02:58:10
Can we attach ng-controller and ng-repeat to the same DOM element? Fiddle Here is the HTML: <table> <tbody ng-controller="UserController" ng-repeat="user in users" ng-click="toggleSelectedUser()" ng-switch on="isSelectedUser()"> <tr> <td>{{user.name}}</td> <td>{{user.email}}</td> </tr> <tr ng-switch-when="true"> <td colspan="2"> {{user.desc}} </td> </tr> </tbody> </table> Here is the code: angular.module("myApp", []) .controller("UserController", ["$scope", function($scope) { $scope.users = [ {name : "Anup Vasudeva", email : "anup.vasudeva2009@gmail.com", desc : "Description about Anup

how to create an angular datepicker directive that uses ng-model

独自空忆成欢 提交于 2019-12-03 02:56:08
I have created an angular directive for my jQuery UI datepicker. The problem is that the directive doesn't update the input's ng-model when a date is selected. Any idea why? http://jsbin.com/ufoqan/1/edit AngularJS actually provides a special controller for interacting with ngModel that you can use inside your directives; just add require: 'ngModel' to your directive definition. This gives you a fourth paramter to your link function, which is the controller you asked for in require --in this case, an instance of ngModelController . It has a method called $setViewValue you can use to set the

angular restriction to not allow spaces in text field

为君一笑 提交于 2019-12-03 02:29:43
I do not want a user to enter spaces in a text field. I don't want it on submit validation but rather - a space will not show up on the text field when they click it. <input ng-model="field" ng-trim="false" ng-change="field = field.split(' ').join('')" type="text"> Update: To improve code quality you can create custom directive instead. But don't forget that your directive should prevent input not only from keyboard, but also from pasting. <input type="text" ng-trim="false" ng-model="myValue" restrict-field="myValue"> Here is important to add ng-trim="false" attribute to disable trimming of an

Testing for focus an AngularJS directive

独自空忆成欢 提交于 2019-12-03 02:28:40
How can you test for focus in an AngularJS directive? I would expect the following to work: describe('focus test', function(){ it('should focus element', function(){ var element = $('<input type="text" />'); // Append to body because otherwise it can't be foccused element.appendTo(document.body); element.focus(); expect(element.is(':focus')).toBe(true); }); }); However, this only works in IE, it fails in Firefox and Chrome Update: The solution by @S McCrohan works. Using this I created a 'toHaveFocus' matcher: beforeEach(function(){ this.addMatchers({ toHaveFocus: function(){ this.message =

angular js templateUrl with absolute path

三世轮回 提交于 2019-12-03 02:16:57
On the video watch page with the url /watch/video_id , I had a ng app. The directive is shown as below. app.directive('myApp', function() { return { restrict: 'E', templateUrl: 'ng-templates/myTemplate.html', link: function(scope, elem, attrs) { }, controller: 'Controller' }; }); Since the templateUrl is the relative path, it will try to find the template in '/watch/ng-templates/myTemplate.html' which is an error. I want to put all the templates in the ng-templates folder. But it won't work if the ng app always looks for the relative path. Is there a way to configure the app making it to look

How to replace an element in AngularJS directive linking function?

霸气de小男生 提交于 2019-12-03 01:52:46
问题 I'm creating a <row> AngularJS directive that needs to replace itself (the <row> tag must not be present in the DOM after execution) with a dynamic template that can contain any HTML code. The problem in using replace: true is that it does not work with table's <tr> tags and that the template is dynamically chosen. So I'm trying to find a way to replace the element in the linking function, with no success. Using jQuery's .replaceWith() breaks ngRepeat for unknown reason. Any hints? Here is

What is the difference between ng-class and ng-style?

南楼画角 提交于 2019-12-03 01:46:32
ng-class and ng-style both seem to be methods of dynamically setting CSS classes. What is the difference between them? ng-style is used to interpolate javascript object into style attribute, not css class. Following directive will be translated to style="color:red" ng-style="{color: 'red'}" And ng-class directive translates your object into class attribute. Following will be translated to class="deleted" when isDeleted variable is true . ng-class="{'deleted': isDeleted}" Note: There is one more use case for ng-style. If you want to interpolate something in style attribute, you should consider