在指令和控制器中使用ng-model [英] Use ng-model in directive and controller

查看:113
本文介绍了在指令和控制器中使用ng-model的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以在Directive中访问链接到Controller的ng-model?

Is it possible to access in Directive the ng-model that is linked to the Controller?

<input type="text" ng-model="ctrl.valuelist.value" />

控制器:

ctrl.valuelist.value = 'initial value';

指令:

function Directive($window) {
  return {
    restrict: 'E',
    scope: {
    },
    controller: 'ctrl as ctrl',
    link: function(scope, elt, attrs){
      // something like this:

      scope.valuelist.value = 'New value';


    }
  };
}


推荐答案

function Directive($window) {
  return {
    restrict: 'E',
    scope: {
      ngModel: '=' //<----- access to the model by this
    },
    controller: 'ctrl as ctrl',
    link: function(scope, elt, attrs){
      // something like this:

      scope.ngModel = 'New value';


    } 
  };
}

我是个傻瓜,所以要善待。

I'm a pleb, so be kind.

这篇关于在指令和控制器中使用ng-model的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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