gpt4 book ai didi

javascript - AngularJS 将 Javascript 对象传递给 Controller

转载 作者:行者123 更新时间:2023-12-02 18:49:41 25 4
gpt4 key购买 nike

我正在尝试将 Javascript 对象传递到我的 AngularJS Controller 中,但没有成功。

我尝试将其传递到 init功能:

<div ng-controller="GalleryController" ng-init="init(JSOBJ)">

在我的 Controller 端:

$scope.init = function(_JSOBJ)
{
$scope.externalObj = _JSOBJ;
console.log("My Object.attribute : " + _JSOBJ.attribute );
};

虽然上面的方法似乎不起作用。

或者,我尝试从我感兴趣的 AngularJS Controller 中提取属性,以便在内联中使用 <script>标签:

var JSOBJ.parameter = $('[ng-controller="GalleryController"]').scope().attribute ;
console.log("My Object.parameter: " + JSOBJ.attribute );

谁能告诉我:这方面的最佳实践是什么?

我无法重写纯 Javascript 对象,因为它是第 3 方库的一部分。

如果我需要提供进一步的说明,请告诉我,并提前感谢您的指导!

--约翰·多伊

最佳答案

尝试将其设置为一个值:

angular.module('MyApp')
.value('JSOBJ', JSOBJ);

然后将其注入(inject)到您的 Controller 中:

angular.module('MyApp')
.controller('GalleryController', ['JSOBJ', function (JSOBJ) { ... }]);

关于javascript - AngularJS 将 Javascript 对象传递给 Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25767327/

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