Use a directive to display scope value and update scope

Ronny vdb

I have an array of vertex values that I loop over and display to the user. The values are stored as a space delimited string, like this:

vrtx = ["10.3 20.3 10.5", "13.2 29.2 3.0", "16.3 2.3 20.2", ...] 

I want to provide a friendly interface to the user to edit the values. This requires me to split the strings into three separate numbers and put them into three individual inputs.

I want to create a directive, as I am not able to store the values as separate values, but after the editing is done, I want to join the values back and update the scope to store the new values as strings.

This is the directive I have so far:

 myApp.directive('fieldValue', function () {

    return {
        scope: {
          field:'='
        },
        link: function (scope, element, attrs) {
            var fields  = scope.field.split(' ');
            element.html("<input type='number' value='"+ fields[0] +"' class='vertex-input'/><input type='number' value='"+ fields[1] +"' class='vertex-input'/><input type='number' value='"+ fields[2] +"' class='vertex-input'/>");    
        }
    }

});

This splits the value and shows the three inputs with the individual values, but my question is how do I join these values and save them back to the scope? Any suggestions would be greatly appreciated! TIA

Pop-A-Stash

You could use formatters/parsers to achieve what you want, but you may have to adjust your input to use ngModel, and I'm not quite sure how it would work with 3 separate input fields:

myApp.directive('fieldValue', function () {

    return {
        scope: {
          field:'='
        },
        require: 'ngModel',
        link: function (scope, element, attrs, ngModel) {
            var fields  = scope.field.split(' ');
            element.html("<input type='number' value='"+ fields[0] +"' class='vertex-input'/><input type='number' value='"+ fields[1] +"' class='vertex-input'/><input type='number' value='"+ fields[2] +"' class='vertex-input'/>");

            ngModel.$formatters.push(function(value){
              //return the value as you want it displayed
            });

            ngModel.$parsers.push(function(value){
              //return the value as you want it stored
            });


        }
    }

});

This way gives you a lot more flexibility in my opinion to use any strategy you want. Read more about it here: https://alexperry.io/angularjs/2014/12/10/parsers-and-formatters-angular.html

And, more official docs here: https://docs.angularjs.org/api/ng/type/ngModel.NgModelController

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Not able to update the `scope` value from directive function

From Dev

Update scope name into directive

From Dev

Change a scope value in directive

From Dev

Use, in child directive, value from parent directive and $scope

From Dev

Use, in child directive, value from parent directive and $scope

From Dev

Update controller scope from directive

From Dev

Getting scope value in child directive

From Dev

Assigning value to scope in directive not working

From Dev

AngularJs directive, scope with value and function

From Dev

Changing a scope value from a directive

From Dev

directive scope rewrite parent value

From Dev

Getting scope value in child directive

From Dev

AngularJs directive, scope with value and function

From Dev

How to use a scope in Directive template

From Dev

AngularJS : directive does not update scope after $http response in parent scope

From Dev

Update angularJs directive scope variable on change parent scope

From Dev

How to update the `$scope` object from `directive` controller

From Java

AngularJS directive does not update on scope variable changes

From Dev

AngularJS : update parent scope from directive

From Dev

Update the Parent Scope from Child Directive

From Dev

Why use scope in directive and how to use it?

From Dev

AngularJS : How to change scope value from isolated scope within directive

From Dev

Directive to directive scope

From Dev

Not able to get the value from the `directive` scope

From Dev

Inject anonymous value into closed directive scope

From Dev

How to set a default value in an AngularJS Directive Scope?

From Dev

Template always compiles with old scope value in directive

From Dev

$scope.$watch not catching the value modified by directive

From Dev

Load angular directive in view, based on $scope value

Related Related

  1. 1

    Not able to update the `scope` value from directive function

  2. 2

    Update scope name into directive

  3. 3

    Change a scope value in directive

  4. 4

    Use, in child directive, value from parent directive and $scope

  5. 5

    Use, in child directive, value from parent directive and $scope

  6. 6

    Update controller scope from directive

  7. 7

    Getting scope value in child directive

  8. 8

    Assigning value to scope in directive not working

  9. 9

    AngularJs directive, scope with value and function

  10. 10

    Changing a scope value from a directive

  11. 11

    directive scope rewrite parent value

  12. 12

    Getting scope value in child directive

  13. 13

    AngularJs directive, scope with value and function

  14. 14

    How to use a scope in Directive template

  15. 15

    AngularJS : directive does not update scope after $http response in parent scope

  16. 16

    Update angularJs directive scope variable on change parent scope

  17. 17

    How to update the `$scope` object from `directive` controller

  18. 18

    AngularJS directive does not update on scope variable changes

  19. 19

    AngularJS : update parent scope from directive

  20. 20

    Update the Parent Scope from Child Directive

  21. 21

    Why use scope in directive and how to use it?

  22. 22

    AngularJS : How to change scope value from isolated scope within directive

  23. 23

    Directive to directive scope

  24. 24

    Not able to get the value from the `directive` scope

  25. 25

    Inject anonymous value into closed directive scope

  26. 26

    How to set a default value in an AngularJS Directive Scope?

  27. 27

    Template always compiles with old scope value in directive

  28. 28

    $scope.$watch not catching the value modified by directive

  29. 29

    Load angular directive in view, based on $scope value

HotTag

Archive