JavaScript Angular: Best practice for updating a variable from a function? angular best practice file structure,angular best practice error handling,

Within an ng-repeat block I have textboxes. To detect when the content differs from the original, the original data is stored in a variable.

<tr data-ng-repeat="p in products">
  <td>
    <textarea data-elastic data-ng-model="p.comment" data-ng-change="hasChanged=checkChange(original, rnd.comment);" data-ng-init="original=rnd.comment; hasChanged=false"></textarea>
  </td>
  <td class="save" ng-show="hasChanged" ng-click="save(p, original)">Save</td>

A save button is shown only when the content has changed. After a successful save the original value should be updated to the new value.

I can do it like this with a function in the controller:

$scope.save = function (p, original) {
  //...successful save
  this.original = p.comment; //this works
  original = p.comment; //this does not
}

Relying on some implicit scope in the form of 'this' doesn't seem sensible.

Why doesn't updating the variable (original = ...) work? What's a smarter way to do this?


Based on comments I've updated it as follows:

ng-click="save(p, this)"

$scope.save = function (p, scope) {
 //...successful save
 scope.original = p.comment; //this works
}

This seems failrly sensible now. Is passing scope around like this considered bad practice or acceptable?


Products is defined as follows:

productStatusApp.controller('productStatusCtrl', function ($scope, $http, cid) {
   $http.get('api/company/products/' + cid).success(function (data) {
      $scope.products = data.products;
   });
Answer:1

I tried document.links and document.anchors in the dev console on a page, and there were 162 links but 255 anchors. However, they are all <a> elements. Up until now, I thought that links and ...

I tried document.links and document.anchors in the dev console on a page, and there were 162 links but 255 anchors. However, they are all <a> elements. Up until now, I thought that links and ...

  1. what is a national emergency
  2. whatsapp
  3. whataburger
  4. what time is it
  5. what is my ip
  6. what song is this
  7. what is a state of emergency
  8. what is a dry cough
  9. what happens in a national emergency
  10. what is the stafford act
  11. what is a bear market
  12. what does ���� mean
  13. what to watch
  14. what stocks to buy today
  15. whataburger menu
  16. what is the weather
  17. what is rsv
  18. what is gluten
  19. what is pv
  20. what is a boomer

I have a plot with many svg rects in my block. Most I were able to plot and fill just fine, however the initial rect is not blue like it is in my color map: var colorMap = { 0: "blue", 1: "red", ...

I have a plot with many svg rects in my block. Most I were able to plot and fill just fine, however the initial rect is not blue like it is in my color map: var colorMap = { 0: "blue", 1: "red", ...

I'm having struggle accomplishing this : I want jQuery to grab class name of each icon and write it in each p element HTML <html> <head> <meta charset="utf-8"> <meta ...

I'm having struggle accomplishing this : I want jQuery to grab class name of each icon and write it in each p element HTML <html> <head> <meta charset="utf-8"> <meta ...

  1. set attribute value jquery
  2. set attribute value javascript
  3. set attribute value
  4. set attribute value in lightning component
  5. set attribute value js
  6. set attribute value dynamically c#
  7. set attribute value in selenium
  8. set attribute value using jquery
  9. set attribute value selenium python
  10. set attribute value c#
  11. set attribute value in lightning controller
  12. set attribute value javascript selenium
  13. set attribute value input javascript
  14. set attribute value using javascript
  15. set attribute value typescript
  16. set attribute value selenium java
  17. set attribute value javascript dynamics crm
  18. set attribute value input jquery
  19. set attribute value in angular 6
  20. set attribute value in oaf

I have the following code: http://jsfiddle.net/chris_bentley/21onnz5a/ <ul id="header-master-fixed-icon"> <li><a href="#" id="header-master-fixed-search" ></a></li> &...

I have the following code: http://jsfiddle.net/chris_bentley/21onnz5a/ <ul id="header-master-fixed-icon"> <li><a href="#" id="header-master-fixed-search" ></a></li> &...

  1. click show hide div
  2. click show hide div jquery
  3. click show hide angular
  4. click show hide javascript
  5. click show hide
  6. click show hide div using css
  7. click show hide css
  8. click show hide toggle
  9. click show hide div jquery demo
  10. button click show hide div
  11. ng-click show hide
  12. button click show hide div in angular 6
  13. vue click show hide
  14. click toggle show hide jquery
  15. checkbox click show hide div
  16. button click show hide div jquery
  17. ng-click show hide div
  18. button click show hide div in angularjs
  19. on click show hide div javascript
  20. js click show hide