gpt4 book ai didi

javascript - Angularjs 自定义指令范围 :true change it's parent scope

转载 作者:行者123 更新时间:2023-11-30 12:03:54 25 4
gpt4 key购买 nike

我试图写一个自定义指令,我把 scope:true。据我所知,这应该采用父范围,当我们改变模型时,它应该只改变它的内部范围。但在下面的示例中,我发现每当我更改指令时,父级也会更改。谁能告诉我背后的原因。

html

<div class="container" ng-app="myModule" ng-controller="mainControll"> <div>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Exercitationem harum numquam odio, tenetur earum deleniti culpa recusandae? Temporibus expedita, porro numquam at voluptas aspernatur nemo veniam nam, vel assumenda placeat!</div>{{text}}<br/> <input type="text" class="form-control" id="exampleInputEmail1" placeholder="Email" ng-model="text"> <firstdir></firstdir> </div>

javascript

var app = angular.module("myModule",[]);
app.controller("mainControll",function($scope){
$scope.text = "Content from controller";
});

app.directive("firstdir",function(){
return{
restrict:"EACM",
replace:"true",
scope:"true",
template:'<p>{{text}}<br/><input type="text" class="form-control" id="exampleInputEmail1" placeholder="Email" ng-model = "text"></p>'
}
})

您可以找到的样本 here

提前致谢

最佳答案

在您提供的代码中,范围被分配了字符串“true”,它实际上应该是一个 bool 值 true

app.directive("firstdir",function(){ return{
restrict:"EACM",
replace:"true",
scope: true,
template:'<p>{{text}}<br/><input type="text" class="form-control" id="exampleInputEmail1" placeholder="Email" ng-model = "text"></p>'}})

关于javascript - Angularjs 自定义指令范围 :true change it's parent scope,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35836965/

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