Search code examples
javascriptangularjsangularjs-scope

Setting up input to update scope variables doesnt seem to work


I have a controller:

.controller("myController", function($scope){
  $scope.name = "Test";
  $scope.desc = "Hello World!";

  $scope.create = function(){
    var test = {name: $scope.name, desc: $scope.desc}
    console.log(test);
  }
}

and the template:

<label class="item item-input item-stacked-label">
    <span class="input-label">{{primaryName}}</span>
    <input type="text" placeholder="{{sampleName}} Name" value="{{name}}">
</label>
<label class="item item-input item-stacked-label">
    <span class="input-label">{{primaryName}}</span>
    <input type="text" placeholder="{{sampleName}} Name" value="{{desc}}">
</label>
<a ng-click="create()">Create Test</a>

It seems that when it runs, the input gets set to Test So i change it to what I want. Alpha and click Create. It will console.log out Test

I dont know if i am doing assignment correctly. I thought I was, but it seems to be otherwise.

What am i doing wrong?

Edit I also attempted to have the create function accept variables, and passed those in, still using the sample: Test/HelloWorld text strings.


Solution

  • <div ng-app="myApp" ng-controller="myCtrl">
    <label class="item item-input item-stacked-label">
        <span class="input-label">{{primaryName}}</span>
        <input type="text" placeholder="{{sampleName}} Name" ng-model="name" >
    </label>
    <label class="item item-input item-stacked-label">
        <span class="input-label">{{primaryName}}</span>
        <input type="text" placeholder="{{sampleName}} Name" ng-model="desc" >
    </label>
    <a ng-click="create()">Create Test</a>
    
    </div>
    

    You are not using ng-model , That's why (y)