gpt4 book ai didi

javascript - 隔离 Angularjs 自定义指令中的范围 - "="

转载 作者:行者123 更新时间:2023-11-28 00:53:18 25 4
gpt4 key购买 nike

我正在尝试为我正在制作的测试应用程序创建一个简单的指令。我的指令如下:

  .directive('answer', function(){
return {
restrict: "E",
scope: {
minl: "=",
model: "="
},
template: '<input id="option" name="opts" type="text" ng-model="model" ng-minlength="minl">'
}

})

在我的 html 文件中,我将此指令与以下代码一起使用:

<answer ng-repeat="option in newQuestion.opts" minl="minLength" model="option.value"></answer>

我的 Controller 作用域上有一个 minLength 属性(等于 10),并且 option.value 在该作用域上也有一个值。但是,当我加载 html 页面时,我的模板不会提取这些值中的任何一个。有人对我在这里可能做错的事情有什么建议吗?

最佳答案

假设您在 Controller 上将 minLength 声明为 int(而不是 Directive 的 Controller ),那么您的代码就完美地用 Angular 编写了。

我在 plunkr 上再次创建了它,所以你可以看一下,除了在 Controller 上声明 minLength 值之外,我没有更改任何内容。 <强> Plunk, Please notice that I called it on my end minl at the controller. thats the only difference

  scope: {
minl: "=",
model: "="
}

这是要走的路。

关于javascript - 隔离 Angularjs 自定义指令中的范围 - "=",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26517353/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com