使用虚拟机不能绑定到范围。表示法控制器/ HTML [英] Can't bind to scope using vm. notation in controller/html

查看:129
本文介绍了使用虚拟机不能绑定到范围。表示法控制器/ HTML的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的路由参数,以获得我所需要的物品的ID建立一个更新的形式。这是工作的打算。具有更新表单的一部分是检索存储的值,并将其绑定到表单。

I am building an update form using a route parameter to get the ID of the item I need. This is working as intended. Part of having the update form is to retrieve stored values and bind them to the form.

如果我有我的模式设置是这样的:

If I have my model set to something like this:

<input type="text" class="form-control" id="Event_Desc" data-ng-model="eventdescription" required />

和我的控制器设置如下(注意,正在调用使用NG-资源REST服务)。

And my controller is set as follows (note, that is calling a REST service using ng-resource).

appItem.get({
        Id: $routeParams.Id
    }, function (result) {
        $scope.eventdescription = result.Title;
    });

它将适当设定的域值存储在表格上的数据库中的值。但是,如果我尝试使用虚拟机。注释如下,则返回无法设置未定义或为空引用属性'eventdescription'的错误

It will properly set the field value to the value stored in the database on the form. However, if I attempt to use vm. notation as follows, it returns an error of "Unable to set property 'eventdescription' of undefined or null reference"

HTML

<input type="text" class="form-control" id="Event_Desc" data-ng-model="vm.eventdescription" required />

控制器:

appItem.get({
        Id: $routeParams.Id
    }, function (result) {
        $scope.vm.eventdescription = result.Title;
    });

我为什么不能设置vm.eventdescription的$范围?

Why can't I set the $scope of vm.eventdescription?

推荐答案

尝试使用它像在此之前实例化虚拟机

try to instantiate vm before use it like this

$scope.vm = {};

appItem.get({
        Id: $routeParams.Id
    }, function (result) {
        $scope.vm.eventdescription = result.Title;
    });

这篇关于使用虚拟机不能绑定到范围。表示法控制器/ HTML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆