值未在控制器中更新

时间:2015-07-17 06:27:22

标签: angularjs

我正在使用具有一组输入框的表单控件。

<form class="form-horizontal" role="form">    
  <input ng-model="editPagename" class="form-control" required/>
  <input ng-model="editUrl" class="form-control" required/>
</form>

editPagename的值未在范围内更新,甚至没有进入该功能。

$scope.$watch('editPagename', function(newVal, oldVal) {
  console.log(newVal);
});

您对如何解决此问题有任何建议吗?

3 个答案:

答案 0 :(得分:0)

$scope.$watch( function() {
 return $scope.editPagename;
}, function(newVal, oldVal) {
    console.log(newVal);
});

答案 1 :(得分:0)

它为我工作。这就是我尝试过的。

HTML:

public void saveAlarm(View v) {

    //set time into calendar instance
    GregorianCalendar calendar= new GregorianCalendar();
    calendar.set(GregorianCalendar.HOUR_OF_DAY,reminderHour);
    calendar.set(GregorianCalendar.MINUTE,reminderMinute);
    calendar.set(GregorianCalendar.SECOND,00);
    calendar.set(GregorianCalendar.MILLISECOND,0000);

    AlarmManager reminder = (AlarmManager)getSystemService(Context.ALARM_SERVICE);

    Intent reminderintent = new Intent(this, AlarmService.class);

    PendingIntent operation = PendingIntent.getActivity(getBaseContext(), 1, reminderintent, PendingIntent.FLAG_UPDATE_CURRENT);
    reminder.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), operation);

    finish();
}

JS:

<div ng-controller="testCtrl">
    <form class="form-horizontal" role="form">    
    <input ng-model ="editPagename" class="form-control" required/>
    <input ng-model="editUrl" class="form-control" required/>
    </form>
</div>

它在控制台中登录。

编辑代码:

最好使用Objec模型而不是原始数据类型。

以下是代码段。

HTML

app.controller("testCtrl",function($scope){ 

 $scope.$watch( 'editPagename', function(newVal, oldVal) {
    console.log(newVal);
   }); 
})

JS:

<div ng-controller="testCtrl">
    <form class="form-horizontal" role="form">    
    <input ng-model ="objEdit.editPagename" class="form-control" required/>
    <input ng-model="objEdit.editUrl" class="form-control" required/>
    </form>
    {{objEdit}}
</div>

})

答案 2 :(得分:0)

因为已经表明$ scope将来会再见(你可以阅读at this "preparing for the future of angularjs" on airpairalso a link the the youtube video of the ng-europe talk),你可能想编写代码以便轻松升级到2 .X

所以,除非你绝对需要$ scope,否则试试这个:

的index.html

<body ng-controller="MainCtrl as vm">
    <p>Hello {{vm.name}}!</p>
    <div>
      <form class="form-horizontal" role="form">    
        <input data-ng-model ="vm.name" data-ng-change="vm.checkValue(vm.name)" class="form-control" required/>
      </form>
    </div>
  </body>

app.js

app.controller('MainCtrl', function() {
  vm = this;
  vm.name = "world";
  vm.checkValue = checkValue;

  function checkValue(value){
    console.log(value);
  }
});

And a plunker link with the example. 不需要$ scope。而我能说的就是你在问什么。