我正在构建一个更新表单,使用路由参数来获取我需要的项目的 ID。这是按预期工作的。更新表单的一部分是检索存储的值并将它们绑定(bind)到表单。
如果我将我的模型设置成这样:
<input type="text" class="form-control" id="Event_Desc" data-ng-model="eventdescription" required />
我的 Controller 设置如下(注意,这是使用 ng-resource 调用 REST 服务)。
appItem.get({
Id: $routeParams.Id
}, function (result) {
$scope.eventdescription = result.Title;
});
它会将字段值正确设置为存储在表单数据库中的值。但是,如果我尝试使用 vm。符号如下,它返回错误“无法设置未定义或空引用的属性'eventdescription'”
HTML:
<input type="text" class="form-control" id="Event_Desc" data-ng-model="vm.eventdescription" required />
Controller :
appItem.get({
Id: $routeParams.Id
}, function (result) {
$scope.vm.eventdescription = result.Title;
});
为什么我不能设置 vm.eventdescription 的 $scope?
最佳答案
尝试在像这样使用之前实例化 vm
$scope.vm = {};
appItem.get({
Id: $routeParams.Id
}, function (result) {
$scope.vm.eventdescription = result.Title;
});
关于javascript - 无法使用 vm 绑定(bind)到范围。 controller/html 中的符号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30814593/