【问题标题】:angularjs $sce is undefined, using angularjs 1.65angularjs $sce 未定义,使用 angularjs 1.65
【发布时间】:2017-08-06 00:30:26
【问题描述】:

我正在使用 angularjs 1.65 版本 似乎 $sce 让我不确定 我一直在尝试 3 个小时以上的时间来修复它。

这里是模块:

var app = angular.module('myApp', ['ui.router','ngclipboard','oitozero.ngSweetAlert']);
app.config(function ($stateProvider, $urlRouterProvider, $locationProvider) {})

这里是控制器:

 app.controller('monitormainController',
 ['$rootScope','$scope','$timeout','SweetAlert','$sce',  function
 ($scope,$timeout,$rootScope,$watch,SweetAlert,$sce) {

  $scope.copyset = function(name){ $scope.trustedHtml =
  $sce.trustAsHtml(name); };

 });

我收到一个错误,提示 $sce 未定义

加载资源失败:服务器响应状态为 404 (找不到文件) angular.js:14642 TypeError: 无法读取属性 未定义的“trustAsHtml” 在 Scope.$scope.copyset (monitormainController.js:93) 在 fn(编译时的评估(angular.js:15500),:4:153) 在回调(angular.js:27285) 在 Scope.$eval (angular.js:18372) 在 Scope.$apply (angular.js:18472) 在 HTMLButtonElement。 (angular.js:27290) 在 HTMLButtonElement.dispatch (jquery.js:5095) 在 HTMLButtonElement.elemData.handle (jquery.js:4766)

很高兴得到任何答复,非常感谢!

【问题讨论】:

    标签: angularjs


    【解决方案1】:

    您的依赖项在这里的顺序错误。您必须将数组中依赖项的顺序与函数中的参数完全匹配。此外,您在函数中期望 $watch,但不将其包含在依赖项列表中。

    编辑:如果您打算使用$scope.$watch 而不是您创建或安装的$watch 服务,则这不是可注入服务。因此,不应将其作为函数参数传递。

    代替:

    ['$rootScope','$scope','$timeout','SweetAlert','$sce',  function
    ($scope,$timeout,$rootScope,$watch,SweetAlert,$sce) {
    

    试试:

    ['$rootScope','$scope','$timeout','SweetAlert','$sce',  function
    ($rootScope,$scope, $timeout, SweetAlert, $sce) {
    

    【讨论】:

    • 仍然无法正常工作 :( ,改变了你的做法。它有​​点连线。idk 怎么说呢,似乎一切都还好。
    • angularjs 安装范围时会为您提供 $watch。从我读到的。
    • 我更新了答案;您很可能希望使用$scope.$watch 而不是一些$watch 服务。删除它应该会使与之相关的任何错误消失。
    猜你喜欢
    • 1970-01-01
    • 2014-10-05
    • 1970-01-01
    • 1970-01-01
    • 2016-11-11
    • 2013-05-28
    • 2015-08-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多