admin管理员组

文章数量:1323744

Is this something one should not do? As I am unable to get it to work.

In my controller I have

$scope.test = "someValue"

and in my view

<input ng-model="test" />

What I am expecting to occur

<input ng-model="someValue" />

However, ng-model stays as being set to "test".

How do I resolve this?

Is this something one should not do? As I am unable to get it to work.

In my controller I have

$scope.test = "someValue"

and in my view

<input ng-model="test" />

What I am expecting to occur

<input ng-model="someValue" />

However, ng-model stays as being set to "test".

How do I resolve this?

Share Improve this question edited Oct 29, 2015 at 17:15 LordTribual 4,2492 gold badges29 silver badges38 bronze badges asked Oct 29, 2015 at 16:50 user2085143user2085143 4,2327 gold badges42 silver badges71 bronze badges 1
  • What is the attribute that you want to set ? or is it that text value be "someValue"? – Harshal Carpenter Commented Oct 29, 2015 at 16:55
Add a ment  | 

2 Answers 2

Reset to default 3

That's not the way ng-model works. If you have a scope variable, as in your case test and value of this variable will reflect in the value property of your input. This means that someValue will be visible in the input. In other words: ng-model is a directive that binds to, e.g. an input,select, textarea (or custom form control) to a property on the scope using the NgModelController.

NgModelController provides API for the ngModel directive. The controller contains services for data-binding, validation, CSS updates, and value formatting and parsing

Here is an example:

var app = angular.module('myApp', []);

app.controller('TestController', TestController);

function TestController() {
  var vm = this;

  vm.myModel = "This is the model value";
  vm.selectedOption = undefined;

  vm.options = [{
    id: '1',
    name: 'Option A'
  }, {
    id: '2',
    name: 'Option B'
  }, {
    id: '3',
    name: 'Option C'
  }];
};
<script src="https://ajax.googleapis./ajax/libs/angularjs/1.4.5/angular.min.js"></script>

<body ng-app="myApp" ng-controller="TestController as vm">
  <input type="text" ng-model="vm.myModel" />

  <select name="mySelect" id="mySelect" ng-model="vm.selectedOption">
    <option ng-repeat="option in vm.options" value="{{option.id}}">{{ option.name }}</option>
  </select>
  
  <pre ng-if="vm.selectedOption">Selected Option: {{ vm.selectedOption }}</pre>
</body>

The example above also shows some best practices, means using controllerAs syntax for your view and a clear declaration of your controller signature.

Show your controller code. However, I have demonstrated below that it should work.

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

angular.module('myApp').controller('myCtrl', function($scope) {
  $scope.test = "somevalue";
});
<script src="https://ajax.googleapis./ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<body ng-app="myApp">
  <form ng-controller="myCtrl">
    <input type="text" ng-model="test" />
  </form>
</body>

ng-model just represents the binding object. It will not change. What changes is the value, which corresponds to the value that the ng-model object takes on.

本文标签: javascriptAngularSet ngmodel using scope variableStack Overflow