admin管理员组文章数量:1276768
I have a list item which toggles modal and sets a param using ng-click
the problem is when calling a function in any other place which logs Course.SelectedCourse
it's undefined although Course.ID
has a value.
<li class="facebook" style="width:33%;">
<a ng-click="Course.SelectedCourse = Course.ID" data-toggle="modal" data-target="#myModal">
<span class="glyphicon glyphicon-user"></span>
</a>
</li>
I have a list item which toggles modal and sets a param using ng-click
the problem is when calling a function in any other place which logs Course.SelectedCourse
it's undefined although Course.ID
has a value.
<li class="facebook" style="width:33%;">
<a ng-click="Course.SelectedCourse = Course.ID" data-toggle="modal" data-target="#myModal">
<span class="glyphicon glyphicon-user"></span>
</a>
</li>
Share
Improve this question
edited May 15, 2016 at 10:56
Mosh Feu
29.3k18 gold badges93 silver badges141 bronze badges
asked Dec 26, 2015 at 23:37
JenuRudanJenuRudan
5451 gold badge14 silver badges30 bronze badges
2
- This take place also after you selected one item of the list, or only the first time when you not yet click a button? – Ignacio Chiazzo Commented Dec 26, 2015 at 23:40
- i clicks that button which opens a modal that has a button that calls a function "dummyfun(Course.SelectedCourse)" where i log (Course.SelectedCourse) – JenuRudan Commented Dec 26, 2015 at 23:42
2 Answers
Reset to default 4Use a function in the controller, this might look like this :
In the view :
<li class="facebook" style="width:33%;" >
<a ng-click="setSelectedCourse(Course.ID)" data-toggle="modal" data-target="#myModal">
<span class="glyphicon glyphicon-user"></span>
</a>
</li>
In the controller
function setSelectedCourse(course_id){
$scope.Course.SelectedCourse = course_id;
}
In AngularJS, directives have their own scope, your list is generated with ng-repeat, isn't it ? That's why
"Course.SelectedCourse = Course.ID"
doesn't work, because inside the div ng-repeat is looping $scope.Course is created locally and isn't the same that the one in your main controller. Besides, functions from main controller can be called by directives, That's why the previous answer works.
本文标签: javascriptAngularJS ngclick with datatoggleStack Overflow
版权声明:本文标题:javascript - AngularJS ng-click with data-toggle - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741238815a2363558.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论