3

I am right now working on a project which has lot of jquery code and i am migrating that to angular, but it needs to deliver as soon as possible(lack of time to change each piece of code).

Code looks like....

var app = angular.module('audiapp', ['ngResource']);

app.controller('audiLayoutCtrl', function ($scope, dataFactory) {

    $scope.editbutton = function () {
        //e.preventDefault();
        var editable = $(this).parents('.editable');
        editable.find('.editable-body').hide();
        editable.find('.editable-form').show();
        editable.find('.editable-toggle').hide();
    };

}

here in this piece of code the editbutton function is getting called but jquery code inside is not working.

I am sorry if i am completly un reasonable as i am very new to angular js.

6
  • how is editbutton called... you have to pass the even object to editbutton using editbutton($event) from your event handler like ng-click="editbutton($event)" Commented Jan 9, 2014 at 3:37
  • Also the controller is not the right place to do dom manipulations, you can use a directive for that Commented Jan 9, 2014 at 3:38
  • I will try with directives. thanks. still not sure how i am gonna do it... Thanks Arun Commented Jan 9, 2014 at 4:00
  • @SamirShah this in editbutton method refers to $scope, not the element that is being clicked. Commented Jan 9, 2014 at 4:40
  • The answer I provided doesn't require a function. Commented Jun 17, 2014 at 16:58

6 Answers 6

3

I think you should change

var editable = $(this).parents('.editable');

to

var editable = jQuery(this).parents('.editable');

$ maybe conflict between Angular and Jquery

Sign up to request clarification or add additional context in comments.

1 Comment

I got this warning message "event.returnValue is deprecated. Please use the standard event.preventDefault() instead"
1

Using Jquery inside the controller is considered as bad practice in Angular JS. The hide and show of particular element is very simple in angular js. You can try like this

HTML

<div class="editable">
    <div class="editable-body" ng-hide="editing"></div>
    <a class="editable-toggle" ng-click="editButton()" ng-hide="editing"></a>

    <form class="editable-form" ng-show="editing"></form>
</div>

Javascript

var app = angular.module('audiapp', ['ngResource']);
app.controller('audiLayoutCtrl', function ($scope, dataFactory, $element) {

  $scope.editing = false;
  $scope.editbutton = function () {
    $scope.editing = true;
  };

}

2 Comments

Yes I know Fizer but there are like 3000+ lines of Jquery Code. I dont have that much time to migrate like this to Angular. Thanks Mate.
I feel the same lot of time to write jquery code inside the controller.Always i feel bad, tried to write everything in directive. But I can understand you. If you want Jquery element, use angular.element('.editable'). It might help you.
1

Old post, but here's another option. Like @derekshull mentioned, you can do this without jQuery. Here's a similar solution utilizing the ng-disabled directive. The form is disabled when you're not editing it, but is still visible if you need it to be.

ng-disabled="!editing"

Example Fiddle

Comments

0

this in editbutton function refers to $scope not the DOM element being clicked. You need a directive so you can easily refer to the element being clicked inside the directive constructor.

Comments

0

Can you try replacing $('.this').parents('.editable'); to $('.editable parent').parents('.editable');. may be $('this') will not point to the exact object inside angular js

var app = angular.module('audiapp', ['ngResource']);

app.controller('audiLayoutCtrl', function ($scope, dataFactory) {

    $scope.editbutton = function () {
        //e.preventDefault();
        var editable = $('.editable parent').parents('.editable');
        editable.find('.editable-body').hide();
        editable.find('.editable-form').show();
        editable.find('.editable-toggle').hide();
    };

}

Note :

I have not tried it . Just a sugesstion.

Comments

0

You don't even need an Angular function to handle this. Just use ng-click and ng-show/ng-hide:

<div class="editable">
    <div class="editable-body" ng-hide="editing"></div>
    <a class="editable-toggle" ng-click="editing=true" ng-hide="editing"></a>
    <form class="editable-form" ng-show="editing"></form>
</div>

So if you click the anchor it sets editing to true, which will hide .editable-body and .editable-toggle...then it will show .editable-form.

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.