0

Alright so I have a function that does a rotation of a string call rotateString() in a file call rot-n.js.

How can I access this function to be called on an ng-model in AngularJS to show the encrypted code in my view?

here is my code:

<html ng-app="cipherApp">
<head>
  <title>ROT-N CIPHER</title>
  <script type="text/javascript" src='https://ajax.googleapis.com/ajax/libs/angularjs/1.5.0/angular.min.js'></script>
  <script type="text/javascript" src="rot-n.js"></script>
  <script type="text/javascript" src="app.js"></script>
  <link href='https://fonts.googleapis.com/css?family=Lato'
rel='stylesheet' type='text/css'>
  <link rel="stylesheet" type="text/css" href="style.css">
</head>


<textarea rows="8" cols="45" ng-model="cipher.encryptIn"></textarea>  <input type="number" min="0" max="25" ng-model="cipher.rotN">
<p>{{rotateString(cypher.encryptIn, cypher.rotN)}}</p>
2
  • So I found an answer to my question located here: stackoverflow.com/questions/23697493/… thanks! Commented Oct 12, 2016 at 1:37
  • mark as answer if my answer has helped Commented Oct 12, 2016 at 1:39

2 Answers 2

0

Create a function on scope that invokes the encryption function:

app.controller("myController", function($scope) {
    $scope.rotFn = function (din) {
        var dout;
        rotateString(din,dout);
        return dout;
    };
});

Then use the ng-change directive to invoke the function:

<div ng-controller="myController">
     <textarea rows="8" cols="45" 
         ng-model="cypher.encryptIn"
         ng-change="cipher.rotN=rotFn(cypher.encriptIn)">
     </textarea>  

    <input type="number" min="0" max="25" ng-model="cipher.rotN">
</div>     
Sign up to request clarification or add additional context in comments.

Comments

0

You cannot access that function when you are using another controller, You should go for Factory or Service

or using Event: listen with $scope.$on and you send it with $scope.$broadcast or $scope.$emit

Sample for factory:

angular.module('share',[])
    .factory('dataService', function() {
       var _dataObj = {};
       return {
        dataObj: _dataObj
      };
    })

    .controller('One', function($scope, dataService) {
      $scope.data = dataService.dataObj;
    })

    .controller('Two', function($scope, dataService) {
      $scope.data = dataService.dataObj;
    });

1 Comment

If the function has of scope exists on and emit to call functions in another controller.

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.