1

I tried to implement for first time the controller as syntax and I face a problem where my function doesn't change the controller variable which I pass from the template. Instead it understands the parameter as a local variable. This wouldn't happen if I use $scope.

Here's my controller.

angular
    .module('app')
    .controller('baseCtrl', baseCtrl);

  function baseCtrl() {

  base = this;
  base.myVar = 'string_1';
  base.myFunction = myFunction;

  function myFunction(value) {
    value = 'string_2"';
  }
}

Here's my template.

<div ng-controller="baseCtrl as base">
  <button ng-click="base.myFunction(base.myVar)">Button</button>
</div>

After click base.myVar should change from "string_1" to "string_2" but this doesn't happen.

Does somebody know how I could figure it out ?

2
  • "This wouldn't happen if I use $scope" .. why not, what would difference be? Your function is still only changing a local variable Commented Sep 8, 2016 at 13:13
  • I think base might be a keyword or something in JavaScript; maybe that has to do with it? Commented Sep 8, 2016 at 13:17

4 Answers 4

1

it can be done like this , do some changes in your code

angular
    .module('app',[])
    .controller('baseCtrl',function(){

   var base = this;
   base.myVar = 'string_1';
  base.myFunction = myFunction;
  
  
  function myFunction(value) {
    value = 'string_2"';
    base.myVar = value
   alert(value);
  }
  
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="app" ng-controller="baseCtrl as base">
  <input type="text" ng-model="base.myVar"/>
  <button ng-click="base.myFunction(base.myVar)">Button</button>
</div>

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

1 Comment

What I need is not to declare base.myVar in the function, but the value to be indexing at base.myVar or any other variable I pass.
1

You have to assign

base.myVar = 'string"';

in myFunction and define module app like this:-

angular.module('app',[]);

This is the modified code that will work.

angular
.module('app', [])
.controller('baseCtrl', baseCtrl);

function baseCtrl() {
    var base = this;
    base.myVar = 'string_1';
    base.myFunction = myFunction;
    function myFunction(value) {
        base.myVar = 'string_2"';
    }
}

Comments

0
base.myVar = "string2"

That will fix it

4 Comments

I need to use this function globally...so everytime I pass a parameter I need to change the relative controller variable.
I see now wot ur tying to do ... Maybe it is working but angular is not updating the view ... Consider using scope.apply
Unfortunatelly it doesn't
yes it does provide an answer. it might be the wrong answer but it sure is an answer
0

I finally figured out what was wrong. The problem is that the variable I pass is primitive. The solution is to change my variables to from primitive to objects.

So in my case the solution is:

base.myVar = {
    val: "string_1"
};

function myFunction(value) {
    value.val = "string_2";
};

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.