[英]How to change angularjs element value within a function?
HTML: HTML:
<div data-ng-app="story" data-ng-init="detail='post a story here'">
<div data-ng-controller="detail_controller">
<input type="text" name="detail" data-ng-model="detail">
<h1>{{detail}}</h1>
<input data-ng-click="submit_detail(detail)" type="submit" name="submit_detail" value="Post detail">
<!--detail dashboard-->
<div ng-model = "detail_dashboard" data-ng-show = "detail_dashboard.show">
<div><h1 data-ng-model = "title" class="title">detail: {{title}}</h1></div>
<div data-posted-answers></div>
</div>
</div>
</div>
Angular js 角js
var app = angular.module('story', []);
function updateArticle(data) {
console.log('updateArticle called '+data);
how to change the value here?
如何在这里更改值?
$rootScope.title = 'hidfg';
}
app.controller('detail_controller', function($scope) {
$scope.detail_dashboard = {
show : false
};
$scope.submit_detail = function($value) {
$scope.detail_dashboard = {
show : true
};
$scope.title = $value;
updateArticle($value);
};
});
$scope and
$rootScope
give this error when used insideupdateArticle
function:当在
updateArticle
函数内部使用时,$ scope和$rootScope
会出现此错误:
ReferenceError: `$rootScope` is not defined
How to change any element value inside a controller? 如何更改控制器内的任何元素值? In jQuery, simply call the element by name, id, class and data attribute.
在jQuery中,只需按名称,id,类和数据属性调用元素。 But angular how can I do that?
但是我该怎么做呢?
I tried this way as well: 我也尝试过这种方式:
angular.element('title').text(''something new);
But that gave me jqLite:nosel
error 但这给了我
jqLite:nosel
错误
Fiddle: https://jsfiddle.net/hadfgy4r/ 小提琴: https : //jsfiddle.net/hadfgy4r/
This is the answer you're looking for: https://jsfiddle.net/27pawemr/2/ 这是您要寻找的答案: https : //jsfiddle.net/27pawemr/2/
var app = angular.module("story", []);
//You can't do this, because you're out of the "Angular consciousness"
/*function updateDashboard(data) {
console.log('updateArticle called '+data);
$rootScope.title = 'hidfg';
}*/
app.service("myModifier", ["$rootScope", function($rootScope) {
this.updateDashboard = function(data) {
console.log('updateArticle called ' + data);
$rootScope.title = 'hidfg';
};
}]);
app.controller("detailController", ["$scope", "myModifier", function($scope, myModifier) {
$scope.detail = "post a story here";
$scope.detailDashboard = {
show: false
};
$scope.submit_detail = function(value) {
//There's no need to redeclare $scope.detailDashboard
$scope.detailDashboard.show = true;
$scope.title = value;
myModifier.updateArticle(value);
};
}]);
Also there's an error in your HTML. HTML中也有错误。 The ng-model works only for inputs!
ng模型仅适用于输入!
<div><h1 class="title">detail: {{title}}</h1></div>
<div data-posted-answers></div>
</div>
So it doesn't make any sense to put an ng-model here 所以在这里放一个ng模型没有任何意义
<div><h1 class="title">detail: {{title}}</h1></div>
it seems like scope() and $apply needed to be used like this. 似乎需要像这样使用scope()和$ apply。 But I need to include jquery for this.
但是我需要为此添加jQuery。 If below code can be modified without jquery, do let me know.
如果可以在不使用jquery的情况下修改以下代码,请告诉我。
function updateArticle(data) {
var scope = angular.element($("#pquestion")).scope();
scope.$apply(function(){
scope.detail_dashboard= {
show : true
};
scope.title= data;
});
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.