Javascript 如何更改函数中的angularjs元素值?
HTML: 如何在此处更改值 $scope和Javascript 如何更改函数中的angularjs元素值?,javascript,angularjs,Javascript,Angularjs,HTML: 如何在此处更改值 $scope和$rootScope在内部使用时给出此错误 updateArticle函数: 如何更改控制器内的任何元素值?在jQuery中,只需按名称、id、类和数据属性调用元素。但是我怎么能做到呢 我也试过这样做: ReferenceError: `$rootScope` is not defined 但这给了我jqLite:nosel错误 Fiddle:似乎需要像这样使用scope()和$apply。但我需要为此包含jquery。如果下面的代码可以在不使用jq
$rootScope
在内部使用时给出此错误
updateArticle
函数:
如何更改控制器内的任何元素值?在jQuery中,只需按名称、id、类和数据属性调用元素。但是我怎么能做到呢
我也试过这样做:
ReferenceError: `$rootScope` is not defined
但这给了我jqLite:nosel
错误
Fiddle:似乎需要像这样使用scope()和$apply。但我需要为此包含jquery。如果下面的代码可以在不使用jquery的情况下修改,请务必告诉我
angular.element('title').text(''something new);
这就是你想要的答案: 你的HTML中也有一个错误。ng模型仅适用于输入
详细信息:{{title}
所以把ng模型放在这里没有任何意义
<div><h1 class="title">detail: {{title}}</h1></div>
<div data-posted-answers></div>
</div>
详细信息:{{title}
小提琴坏了。请修理。我将尝试在不使用$apply的情况下解决它,这是有风险的。这应该是什么<代码>angular.element($(“#pquestion”)).scope()代码>
ReferenceError: `$rootScope` is not defined
angular.element('title').text(''something new);
function updateArticle(data) {
var scope = angular.element($("#pquestion")).scope();
scope.$apply(function(){
scope.detail_dashboard= {
show : true
};
scope.title= data;
});
}
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);
};
}]);
<div><h1 class="title">detail: {{title}}</h1></div>
<div data-posted-answers></div>
</div>
<div><h1 class="title">detail: {{title}}</h1></div>