简体   繁体   English

Angular JS双向绑定不起作用

[英]Angular JS two-way-binding doesn't work

I am not able to use two-way-binding in my controller. 我无法在控制器中使用双向绑定。 There is something strange but I don't understand it. 有一些奇怪的东西,但我不明白。 My data structure is a javascript object containing an array of strings. 我的数据结构是一个包含字符串数组的JavaScript对象。

$scope.schedule = {
    schedule_name: "Test 123",
    schedule_id: 1234,
    dates: [{
        date: "2015-01-01"
    }, {
        date: "2015-02-01"
    }, {...}]
}

I retrieve the schedules from my database and place it in a list in the UI. 我从数据库中检索计划,并将其放置在UI中的列表中。 The elements of the $scope.scheduleList are clickable. $scope.scheduleList的元素是可单击的。 If the user clicks on a schedule element then it will appear on the screen with all properties of the schedule object. 如果用户单击计划元素,则它将显示在屏幕上,其中包含计划对象的所有属性。

I iterate over the dates array with ng-repeat="date in schedule.dates" and use the date-value in ng-model="date.date" like this. 我使用ng-repeat="date in schedule.dates"的date遍历dates数组,并使用ng-model="date.date"的date-value这样。

My Problem is that the scope variable schedule won't be updated on any changes. 我的问题是范围变量schedule不会在任何更改上进行更新。 Here is an excerpt of my code: 这是我的代码的摘录:

 $scope.scheduleList = []; CommonDataService.get(URL).then(function(data) { for (var i = 0; i < data.length; i++) { data[i].dates.forEach(function(value, key) { data[i].dates[key] = {date: $filter("date")(value, "EEE, dd.MM.yyyy")}; }); data[i].schedule_name = data[i].schedule_name.substr(4); } $scope.scheduleList = data; }); $scope.schedule = {}; $scope.showSchedules = function(schedule) { $scope.schedule = schedule; } ... $scope.save = function() { console.log($scope.schedules); //CommonDataService.add(URL, $scope.schedules).then(function(response) { // console.log(response); //}); }; 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script> <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.9/angular.min.js"></script> <body data-ng-app="rwk"> ... <div class="form-group col-lg-6" data-ng-repeat="date in schedule.dates track by $index" data-ng-if="key !== 'schedule_id' && key !== 'schedule_name'"> <div class="col-lg-9"> <p class="input-group pull-left"> <input data-rwk-datepicker data-format="D, dd.mm.yyyy" id="{{$index}}" type="text" class="form-control" data-ng-model="date.date" data-ng-required="true" /> </p> </div> </div> </body> 

I am very lucky if anyone could help me! 如果有人可以帮助我,我很幸运! Thanks in advance! 提前致谢!

You are likely calling the showSchedules() function from outside angular's scope. 您可能是从超出angular范围的范围调用showSchedules()函数。 You need to tell angular to update the bindings by wrapping the function body in $scope.$apply(...) : 您需要通过将函数主体包装在$scope.$apply(...)来告诉angular更新绑定$scope.$apply(...)

$scope.schedule = {};
$scope.showSchedules = function(schedule) {
    $scope.$apply(
        $scope.schedule = schedule;
    );
}

You could wrap the call to showSchedules in a $scope.$apply(...) call as well. 您也可以showSchedules的调用showSchedules$scope.$apply(...)调用中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM