gpt4 book ai didi

javascript - 为什么我们需要在这里使用$scope?

转载 作者:行者123 更新时间:2023-11-28 19:40:09 25 4
gpt4 key购买 nike

(function() {
var app = angular.module("someawesomemodule", []);

app.factory('data', function(){
return { sometest: 'x' };
});

app.controller("FirstController", function( $scope, data ) {
$scope.data = data;
$scope.data.sometest = "a";
});
})

为什么这不起作用?

(function() {
var app = angular.module("someawesomemodule", []);

app.factory('data', function(){
return { sometest: 'x' };
});

app.controller("FirstController", function( data ) {
data.sometest = "a";
});
})

什么是$scope..?

最佳答案

$scope 是 View 和 Controller 之间的粘合剂。如果你想在这些之间共享数据,你必须使用。但是你可以使用以下语法:

app.controller("FirstController", function( data ) {
this.data.sometest = "a";
});

但这不是最好的场景:)使用 $scope 的最佳方式:

(function() {
var app = angular.module("someawesomemodule", []);

app.factory('data', function(){
return { sometest: 'x' };
});

app.controller("FirstController", function( $scope, data ) {
$scope.data = data;
$scope.data.sometest = "a";
});
})

在 View 中:

<div ng-controller="FirstController">
{{data.sometest}}
</div>

This将是思考 Scope 和 Angular JS 的良好开端。如果您开始学习 AngularJS,请开始阅读 Angular JS 页面中的教程,然后阅读一些设计内容,例如 this

关于javascript - 为什么我们需要在这里使用$scope?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25243124/

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