[英]AngularJS - change controller model in directive
我认为您使用了错误的语法来引用ngModel,并结合了Angular可能已经使用了ngModel的事实。 如果您更改用于存储文本的属性的名称,即txtVal
,并且在没有scope.
情况下进行引用scope.
在模板中,您应该能够在单击跨度时更改输入的值。
像这样:
<div ng-controller="ctrl">
<input change-model txt-val="change_me" />
<p>{{change_me}}</p>
并在js文件中:
app.directive "changeModel", ($compile) ->
priotity: 0
restrict: "A"
scope:
txtVal: '='
link: (scope, element, attrs) ->
//Use txtVal instead of scope.txtVal here
tpl = "<input ng-model='txtVal' /><span data-value='Write this to model' ng-click='clickHandle()'>Must paste text on click</span></span>"
new_el = angular.element(tpl)
scope.clickHandle = ->
scope.txtVal = "some text"
通过这些更改,单击范围将同时更新输入的值和<p>
的内容
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.